Find Related products on Amazon

Shop on Amazon

Perfect Random Floating-Point Numbers

Published on: 2025-07-19 05:56:12

When I recently looked at the state of the art in floating point random number generation, I was surprised to see a common procedure in many programming languages and libraries that is not really a floating-point algorithm: Generate a random integer with bits chosen based on the precision of the format. Convert to floating point. Divide to produce an output between 0 and 1. In code, this looks like: 1 func ( r * Rand ) Float64 () float64 { 2 int64 rand_int = r . Int63n ( 1 << 53 ) 3 return float64 ( rand_int ) / ( 1 << 53 ) 4 } This function is supposed to produce floating-point numbers drawn from a uniform distribution in the interval $[0, 1)$. Zero is a possible output, but one is not, and the distribution is uniform. The number "53" in the algorithm above is chosen in a way that is floating-point aware: the double-precision floating-point numbers have 53 bits of precision, so this algorithm only creates bits equal to the precision of the number system. It seems to fit the bill. ... Read full article.