1

I've been trying to create a class to roll dice for games, and my code for one aspect of it is:

public int[] yahtzeeRoll() {
    int[] rolls1 = new int[6];
    for (int i = 0; i < 6; i++) {
        rolls1[i] = ((int) Math.random()*6+1);
    }
    return rolls1;
}

yet, when I call it in the main method, it only returns 1 for each of the values. Why is this? How can I fix my code so that it generates 6 different numbers when I print the array in the main method?

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
helpme
  • 29
  • 5

6 Answers6

1

You are casting the double value returned by Math.random() to int before multiplying by 6, and since Math.random() returns a value < 1, casting it to int results in 0.

Change

rolls1[i] = ((int) Math.random()*6+1);

to

rolls1[i] = (int)(Math.random()*6)+1;
Eran
  • 387,369
  • 54
  • 702
  • 768
0

The type casting by appending (type) takes precedence over the * 6 bits afterwards. Therefore, the result from Math.random() is always casted into 0 before you multiply it by 6, which turns out to always be 0 as well.

This answer points to this site which explains it quite well.

Either (int) (Math.random() * 6) + 1 or (int) (Math.random() * 6 + 1) would work as you have intended.

T Tse
  • 786
  • 7
  • 19
0

Math.random returns a floating point number between 0 and 1 but you are truncating it down to 0 by using (int) type cast before it. Use parentheses around your expression and then prepend (int) to that if you do wish to use integer truncation.

Btw, I think same sequence should be generated at each run if you don't seed the pseudo-random engine, say with current time or something.

George Birbilis
  • 2,782
  • 2
  • 33
  • 35
0

Math.random() returns double value in range [0, 1) (greater than or equal to 0.0 and less than 1.0). Then you cast that double value to int, so it always results in 0. After that you add 1 to it, so the result always remains 1.

You should cast result of multiplication - Math.random() * 6 instead of casting Math.random() return value:

rolls1[i] = (int)(Math.random()*6)+1;

By the way, you should be aware of operators precedence in Java language. You can have a look here: operator precedence to see nice table that shows that casting has a higher priority than multiplication and addition - this is the reason, why (Math.random()*6) is put in parenthesis for casting (this way you avoid casting only Math.random())

PS. There is also a link to table of operator precedence in official Java tutorial, but it doesn't exactly fit to your problem as it doesn't contain operation of casting - this is the reason, why I provided another link firstly.

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
0

Let's look at the expression.

((int) Math.random()*6+1)

Now Math.random() returns a double that is >=0 and <1.

You then cast that result to an int which means it will always become 0.

If you use.

(int)(Math.random()*6+1)

You are taking the double between 0 and 1, multiplying it by 6 (giving 0 ... 6) adding 1 and then casting to an int. This looks more like what you are looking for.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

You can take a clue from the below output presentation which is self explanatory. Code:

double random = Math.random();
System.out.println("Math.random()>>"+random);
System.out.println("Math.random()*6>>"+random*6);
System.out.println("(int)(Math.random()*6)>>"+(int)(random*6));
System.out.println("Math.random()*6+1>>"+random*6+1);   //+1 here is treated as a string by java and will add at the end of the number
System.out.println("(Math.random()*6+1)>>"+(random*6+1));   //number random*6 will be incremented by 1 as enclosing () will treat them as numbers
System.out.println("(int)(Math.random()*6+1)>>"+(int)(random*6+1));

Output:

Math.random()>>0.6793602796545469
Math.random()*6>>4.076161677927281
(int)(Math.random()*6)>>4
Math.random()*6+1>>4.0761616779272811
(Math.random()*6+1)>>5.076161677927281
(int)(Math.random()*6+1)>>5
Suresh
  • 1,491
  • 2
  • 22
  • 27
  • @PrzemysławMoskal 3.93... is a calculator answer and I agree. Java code only adds 1 to 2.9370739993590416 and returns 2.93707399935904161, check the 1 added at the end of the result. – Suresh Feb 08 '18 at 10:11
  • It would act like you say if would take String representation of that value (e. g. String myString = String.valueOf(Math.random) + 1 - in that case +1 means that you concatenate result with 1 at the end. When working with double +1 gives 3.93.... – Przemysław Moskal Feb 08 '18 at 10:23
  • @PrzemysławMoskal that is how exactly I have done. Sharing my code here FYR. double random = Math.random();System.out.println("Math.random()*6+1>>"+random*6+1); – Suresh Feb 08 '18 at 10:29
  • 1
    Try this: double random = Math.random(); System.out.println("Math.random()*6+1>>" + (random*6+1)); <- this will make operation inside parenthesis perform as mathematical operation, not as String concatenation. Sorry for the previous comment - SO Android App changes quotes to strange texts when I'm editing my comment. – Przemysław Moskal Feb 08 '18 at 11:10
  • 1
    @PrzemysławMoskal you are right, ()s are doing all the difference, edited my answer accordingly. Thank you for highlighting this.+1 to you :) – Suresh Feb 08 '18 at 11:15