In this comment in another question, dimo414 states that one of the problems with the OP's code is that it will generate random numbers non-uniformly. So I wonder why this is? Is it inherent the the particular algorithm? Or is it something about Math.random() itself? Is it because of the floating-point representation of the numbers chosen in the interval [0.0, 1.0)?
p.s. I understand the proposed answer to use Random.nextInt(). I also want to know more about the flaws of using Math.random().
解决方案
Java's Math.random() is documented to return numbers that "are chosen pseudorandomly with (approximately) uniform distribution from" the interval [0, 1). However, although that documentation specifies that it uses its own java.util.Random instance, it's not documented how it uses that instance to generate random numbers. For example, it's not documented whether that method will call the nextDouble method, which is exactly specified for java.util.Random.