math.random, only generating a 0?

You are using Math.random() which states

Returns a double value with a positive sign, greater than or
equal to 0.0 and less than 1.0.

You are casting the result to an int, which returns the integer part of the value, thus 0.

Then 1 + 0 - 1 = 0.

Consider using java.util.Random

Random rand = new Random();
System.out.println(rand.nextInt(3) + 1);

Leave a Comment