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