6 Replies Latest reply on Jul 2, 2010 4:05 AM by Drith

# Random number "Dice"

I need a script that Rolls 2 6 sided dice and subtracts the larger number from 20 , if both roll 6 roll an additional 4 sided die and add that outcome to 6 then subtract from 20. if both roll 1 subtract 2 from 20.

I was given this script but it doesn't seem to work correcly. Can anyone help me?

import java.util.Random

public static void main(String[] args) {
int x;
Random r = new Random();
int d1 = r.nextInt(1,6), d2 = r.nextInt(1,6), d3 = r.nextInt(1,4);
if(d1 = 6 & d2 = 6) x = 20 - (6 + d3);
else if(d1 = 1 & d2 = 1) x = 18;
else if(d1 > d2) x = 20 - d1;
else if(d1 < d2) x = 20 - d2;
}

• ###### 1. Re: Random number "Dice"

The comparison operator in JS is == , not = .

• ###### 2. Re: Random number "Dice"

This was the original script I was given and it also didn't work the firsto ne i posted my roomate edited.

`import java.util.Randompublic static void main(String[] args) {    int x;    Random r = new Random();    int d1 = r.nextInt(1,6), d2 = r.nextInt(1,6), d3 = r.nextInt(1,4);    if(d1 == 6 && d2 == 6) x = 14 - d3;    else if(d1 == 1 && d2 == 1) x = 18;    else if(d1 > d2) x = 20 - d1;    else if(d1 < d2) x = 20 - d2;}`

• ###### 3. Re: Random number "Dice"

I didn't pay much attention before, but this is Java code, not JavaScript. It won't work at all.

What's the context of this script?

• ###### 4. Re: Random number "Dice"

I am making a button to generate a number and place it in a specific form field. I want it to use the description i first posted. and have it do it 6 times each for a different field.

EDIT: This better describes what I am looking for.

On button push:

Die1 = Random number 1-6
Die2 = Random number 1-6
Die3 = Random number 1-4

If Die1 = 6 AND Die2 = 6
then subtract (Die3 + 6) from 20 then send to field "x"

if Die1 = 1 AND Die2 = 1
then send 18 to field "x"

if Die1 > Die2
then subtract Die1 from 20 then send to field "x"

if Die1 < Die2
then subtract Die2 from 20 then send to field "x"

• ###### 5. Re: Random number "Dice"

You will find how JavaScript generates random numbers in Core JavaScript 1.5 Reference along with other JavaScript syntax, objects, properties, methods, statements, etc. Documents for JavaScript for Acrobat are located at the Adobe Acrobat Developer site. JavaScirpt used 'Math.random' method.

• ###### 6. Re: Random number "Dice"

This works!

var a = this.getField("x");

var D1 = Math.round((Math.random()*5)+1);

var D2 = Math.round((Math.random()*5)+1);

var D3 = Math.round((Math.random()*3)+1);

var D0 = D1+D2;

if (D0==2) {a.value=20-D0}

else if (D0==12) {a.value=20-(D3+6)}

else if (D1>=D2) {a.value=20-D1}

else if (D1<D2) {a.value=20-D2}

Message was edited by: Drith