Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

40

Likes

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

Adobe Community Professional , 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.

Likes

Translate

Translate
Adobe Community Professional , 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;

Likes

Translate

Translate
Adobe Community Professional ,
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.

Likes

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?

Likes

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
Adobe Community Professional ,
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;

Likes

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