• Global community
• Language:
• Deutsch
• English
• EspaÃ±ol
• FranÃ§ais
• PortuguÃªs
• æ—¥æœ¬èªžã‚³ãƒŸãƒ¥ãƒ‹ãƒ†ã‚£
Dedicated community for Japanese speakers
• í•œêµ­ ì»¤ë®¤ë‹ˆí‹°
Dedicated community for Korean speakers
Exit
• Re: Help with javascript

Help with javascript

Community Beginner ,
Sep 11, 2021 Sep 11, 2021

Copied

I have a script to roll six side dice,
var roll = Math.random() * 6+1;
this.getField("Dice").value = roll;
This may sound complicated but I want to set percentage to get 6 to be more then other numbers, is that possible with javascript?

TOPICS
How to , JavaScript

Views

301

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Community Expert , Sep 11, 2021 Sep 11, 2021

First of all use Math.floor() to get integer otherwise you won't get whole number.
Secondly it's not that complicated at all, just add more numbers to die and use if else to set extra numbers to be 6,
for example lets use 8 numbers in a die, use script like this:

var roll = Math.floor(Math.random() * 8)+1;
if(roll == 7 || roll == 8)
this.getField("Dice").value = 6;
else
this.getField("Dice").value = roll;

If die rolls 7 or 8 it will show as six.

Community Expert , Sep 11, 2021 Sep 11, 2021

Use this:

var roll = Math.floor(Math.random() * 30)+1;
if(roll > 20)
this.getField("Dice").value = 20;
else
this.getField("Dice").value = roll;

3 Replies 3
Community Expert ,
Sep 11, 2021 Sep 11, 2021

Copied

First of all use Math.floor() to get integer otherwise you won't get whole number.
Secondly it's not that complicated at all, just add more numbers to die and use if else to set extra numbers to be 6,
for example lets use 8 numbers in a die, use script like this:

var roll = Math.floor(Math.random() * 8)+1;
if(roll == 7 || roll == 8)
this.getField("Dice").value = 6;
else
this.getField("Dice").value = roll;

If die rolls 7 or 8 it will show as six.

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Sep 11, 2021 Sep 11, 2021

Copied

Thank you so much. Another question if I have dice with 30 numbers and I want to make 20 and above all roll 20?

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Expert ,
Sep 11, 2021 Sep 11, 2021

Copied

LATEST

Use this:

var roll = Math.floor(Math.random() * 30)+1;
if(roll > 20)
this.getField("Dice").value = 20;
else
this.getField("Dice").value = roll;