Random Floating-Point Values

Generating Pseudo-random Floating-Point Values

by Allen B. Downey

This is a draft of a paper I think I will probably not publish, but I think it is kind of interesting, so I thought I would put it out there. If nothing else, it contains some pedagogical material about IEEE floating-point representation.

Here is the paper in PDF, Compressed Postscript, and Postscript.

And here is C code that implements several versions of my algorithm:


The conventional method for generating a pseudorandom floating-point value is to generate a pseudorandom integer and divide by a constant (using floating-point arithmetic). The problems with this approach are (1) the possible outcomes are a small subset of the representable floating-point values (about $7%$ for a typical implementation) and (2) subsequent transformation of these values may not yield the expected distributions (for example, applying a log transformation to numbers from a uniform distribution does not yield an exponential distribution as it would in real arithmetic). We present a new algorithm with the following properties: (1) it can produce every representable floating-point value in a given range, and (2) it is efficient in both time and use of pseudorandom bits.