views:

241

answers:

8

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 because 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.

+9  A: 

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 :-).

Matt Solnit
+7  A: 

It's inclusive of the zero, exclusive of the one, e.g., [0, 1) or 0 <= x < 1 depending on which notation you prefer.

Hank Gay
A: 

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 =)

Viktor Sehr
+1  A: 

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.

Mike
A: 

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

Stephen P
+4  A: 

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).

Ira Baxter
You could make the same case for any combination of the 56 bits of internal state. If the random number generator is implemented well, the chance of returning zero ought to be the same as any other discrete double.
Gilbert Le Blanc
@Gilbert: Agreed, but the question was specifically "can it be zero?"
Ira Baxter
Java uses a 48-bit LCG of which only 32 bits are used. This is also noted in the documentation for `nextLong()` that it simply *can't* generate all possible `long` values. Same goes (obviously) for `double`s.
Joey
@Johannes: OK, so the random numbers aren't as random as they could be (32 bits instead of 56). Seems like a pretty dumb implementation; what's the point of a random number generator that isn't very random? Still only 1 chance in 2^32 or less than 1 in a billion.
Ira Baxter
Never expect much from built-in PRNGs of languages. LCGs are common and sometimes you're even lucky if they have a full period. If you do simulations you need more than one PRNG anyway and for most applications the supplied one is enough. Most people also won't notice the difference between “0 appears once in 2^53 tries” and “0 won't ever appear” – in practice both of these are likely to be the same result. Also, at the time Java was created, there were few actually good algorithms – there were better but hey, if you need it, implement you own. That's the point of a programming language.
Joey
People have been building random number generators for decades; there was tons of theory when I looked at in a numerical algorithms class back in the 70s. How to build a good one was easily available when Java came out. Sounds to me like they simply did a lousy job. Yes, you can code your own. The point of a framework or library is to arrange it so you don't have to do that.
Ira Baxter
+1  A: 

it is also possible, in a compliant JRE implementation, that it NEVER returns 0.

irreputable
it is also possible, given a truly random generator and due to the nature of randomness that, in a compliant JRE implementation, it ALWAYS returns 0.
Stephen P
@Stephen: Read the Javadocs (which, for the standard library are considered part of the specification). Any implementation of `Math.random()` *has to* use `java.util.Random` whcih in turn *has to be* a specific LCG.
Joey
@Johannes: I read the javadocs, and linked to them in my answer. My point, relating to irreputable's answer, is that while it's true it is *possible* that a 0.0 is never returned it is also possible that a 0.0 is *always* returned. To be compliant it must be possible for any value *x* where 0.0 <= *x* < 1.0 to be returned, including the improbable cases where 0 is never returned or 0 is always returned.I first read irreputable's statement as "you could write a compliant JRE that would never-ever return 0." which is wrong, but the *improbable* case that 0 is never returned *is* correct.
Stephen P
+5  A: 

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 s test case to work out. And that might mean that that exact point in time could be a few decades or millenia away.

Joey
+1 I always wondered if I would ever get a zero too....
mikera