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

Help with javascript

Community Beginner ,
Sep 11, 2021 Sep 11, 2021

Copy link to clipboard

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

386

Translate

Translate

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 guidelines

correct answers 2 Correct answers

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.

Votes

Translate

Translate
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;

Votes

Translate

Translate
Community Expert ,
Sep 11, 2021 Sep 11, 2021

Copy link to clipboard

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.

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Sep 11, 2021 Sep 11, 2021

Copy link to clipboard

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?

Votes

Translate

Translate

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 guidelines
Community Expert ,
Sep 11, 2021 Sep 11, 2021

Copy link to clipboard

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;

Votes

Translate

Translate

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 guidelines