
Math.random generating 0
Hi all,
I have create the following code in the hope of producing four random numbers from 1 to 6.
Sometimes a '0' is returned, but I can't figure out why! :o
Code:
for (int i = 1; i <= 4; i++ ) {
int num = (int)(Math.random() * (1 + 6));
System.out.print(num);
}
Can someone please help me?
Many thanks!

Math.random returns a number from 0.0 to less than 1.0, which means there is more than a ten percent chance that a number smaller than 0.142 (approximately a 14.2% chance, obviously) and that number times 7 (why you do 1+6 there I don't know) is still less than 1.0 and casting a number smaller than 1 to int will produce 0.
Edit: Use the Random class and "nextInt(6) + 1"