Generating Uniform Random Numbers on [0,1]
Chris Lomont, March 2017
Common advice for generating uniform random numbers in [0,1] in many languages looks like this [1,2,3]:
double value = ((double)rand())/(RAND_MAX);
This however does not generate many possible floating-point numbers in $[0,1]$, leaving gaps – there are many floating-point numbers that should occur but cannot due to this method. To understand why, you need to understand how floating-point numbers are (usually) stored on computers.
Throughout we’ll assume the random number generators produce uniformly random integers in a known range.