Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help with javascript

Community Beginner ,
Sep 11, 2021 Sep 11, 2021

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
705
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
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.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 11, 2021 Sep 11, 2021
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;

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
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.

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

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 11, 2021 Sep 11, 2021
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;

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