fanf: (Default)
[personal profile] fanf

https://dotat.at/@/2023-06-23-random-more.html

I got some interesting comments about my previous notes on random floating point numbers on Lobsters, Dreamwidth, and from Pete Cawley on Twitter.

Here's an addendum about an alternative model of uniformity.

There are 2^62 double precision floats between 0.0 and 1.0, but as I described before under "the problem", they are not distributed uniformly: the smaller ones are much denser. Because of this, there are two ways to model a uniform distribution using floating point numbers.

Both algorithms in my previous note use a discrete model: the functions return one of 2^52 or 2^53 evenly spaced numbers.

You can also use a continuous model, where you imagine a uniformly random real number with unbounded precision, and return the closest floating point result. This can have better behaviour if you go on to transform the result to model different distrbutions (normal, poisson, exponential, etc.)

Taylor Campbell explains how to generate uniform random double-precision floating point numbers with source code. Allen Downey has an older description of generating pseudo-random floating-point values.

In practice, the probability of entering the arbitrary-precision loop in Campbell's code is vanishingly tiny, so with some small adjustments it can be omitted entirely. Marc Reynolds explains how to generate higher density uniform floats this way, and Pete Cawley has terse implementations that use one or two random integers per double. (Reynolds also has a note about adjusting the range and fenceposts of discrete random floating point numbers.)

Date: 2023-06-24 22:02 (UTC)
bens_dad: (Default)
From: [personal profile] bens_dad
Do you actually want random floating-point numbers uniformly distributed in [0,1) ?
Fixed-point would give you uniform distribution, possibly with only an implied division.
If you want numbers in [p,q] or [p,q) would it not be better to address the uniformities on that range ?
If you wanted random floats for their "floatiness", do you want them to be uniform in the real line rather than in the space of possible bit patterns ? The only use of "floatiness" that springs to mind is fuzzing, but that does not want uniformity as we are especially interested in edge cases when fuzzing.

June 2025

S M T W T F S
1234567
8 91011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated 2025-06-10 12:56
Powered by Dreamwidth Studios