Can Java's random function be zero?
Just out of curiosity, can Math.random() ever be zero?
For example, if I were to have:
while (true){
if (Math.random() == 0)
return 1;
}
Would I ever actually get a return of one? There's also rounding error to consider becau开发者_Go百科se Math.random() returns a double.
I ask because my CS professor stated that random() goes from 0 to 1 inclusive, and I always thought it was exclusive.
Yes, it really can be. Math.random()
creates a global java.util.Random
-generator with seed (System.currentTimeMillis() ^ 0x5DEECE66DL) & ((1L << 48) - 1)
and calls nextDouble()
for it. If its seed reaches state 107048004364969L
(and it will, since java.util.Random
has full period), the next double
generated will be 0.0
.
Though with bad luck you could end up with the wrong parity in the cycle, because
Random.nextDouble()
advances the state twice. With little less bad luck you could have to generate 2^47 random numbers before the loop terminates, as I didn't find any other seeds that give 0.0
.
The seed advances as if by seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
and doubles are generated using 26 and 27 upper bits of two consecutive seed values. In the example the two next seed values will be 0L
and 11L
.
If you manage to create the global generator with System.currentTimeMillis()==107038380838084L
, your code returns immediately. You can simulate this with:
java.util.Random k = new java.util.Random(107038380838084L);
System.out.println(k.nextDouble()==0);
According to the documentation, "Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0." This means it can be zero.
As Hank wrote, it is exclusive on the upper boundary (can never be 1), so maybe that's where your confusion comes from :-).
It is perfectly possible that it will never return exactly zero. Java's included PRNG is a 48-bit LCG from which only 32 bits are ever used. For all 53 bits of a double
mantissa to be zero, you'll essentially need at least one call to next()
where the upper 32 bits are zero and another where most of them are. (If I'm not mistaken, I'd say this won't ever happen with how the generator works, but it's late, I'm tired, and I won't bet much on it.)
Since the method documentation explicitly states how random numbers are obtained there is also little leeway for other implementations of the Java runtime to yield different results. The contract might say that the number you get is from [0, 1). But in practice there are quite a number of values you'll never hit (because you need two successive values from a generator that foribly yields a linear dependency between successive values – there are only 48 bits of state. You can't generate all different 53-bit combinations from that – at least not how it's done.).
Of course, since Math.random()
automatically seeds a static Random
instance, we might also have to consider the seed here, which may need to be very specific for a test case to work out. And that might mean that that exact point in time could be a few decades or millennia away.
It's inclusive of the zero, exclusive of the one, e.g., [0, 1)
or 0 <= x < 1
depending on which notation you prefer.
In theory, it can return the value zero.
In practice, you might have to wait an extremely long time to get exactly zero. If the random number generator is implemented well, it has at least 56 bits of internal state (otherwise all the bits of the returned result will not be random). And that implies, if the distribution of the values produced by random is flat, that you have at most one chance in 2^56 of getting back a value all of whose bits are zero. That's roughly 10^-19. I wouldn't hold my breath.
(Others have rightfully observed that as documented, in theory [and presumably in practice] it cannot return the value 1.0).
From the java API.
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#random()
So yes it can be.
Math.random() is documented to return "a double value with a positive sign, greater than or equal to 0.0 and less than 1.0" That is, inclusive of 0.0 but exclusive of 1.0
it is also possible, in a compliant JRE implementation, that it NEVER returns 0.
It's theoretically possible for math.random() to return a zero value, but in the real world, you can count on it virtually never happening.
I once ran my pc for a week straight waiting for one, it generated about ten trillion random numbers, no zeros.
But more directly, it is practically exclusive both ways.
From http://java.sun.com/javase/6/docs/api/java/lang/Math.html
random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Yes, it can be zero, but not 1. In other words, prefer the Java documentation over your CS professor =)
精彩评论