2026-10-03 22:32 UTC
What's the right way to generate a floating-point number representing a sample of a uniformly random variable in the range [0,1]?
You might think: choose the exponent to be 2⁻¹ with probability 1/2, 2⁻² with probability 1/4, etc (with some fudge when the range runs out). And then, once you've chosen an exponent, choose uniformly at random from the mantissas.
Wrong!
The #IEEE754 ethos is to calculate the mathematically perfect result, and then round that to the nearest representable number. So you must choose each floating-point number x with probability in proportion to its rounding 'catchment area': the range of real numbers above and below x that will round to x.
Well, isn't that what I just said?
No, it isn't – because powers of 2, at the boundary between two exponents, have a catchment area half as big on the left as on the right. So you must choose _most_ numbers with probability 2⁻ⁿ where n varies with the exponent, except that powers of 2 are chosen with probability 2⁻ⁿ + 2⁻ⁿ⁺¹. Arrgh!
Replies (0)
No replies.