0

How to generate floats which are uniform w.r.t. the continuous number line? (i.e., a number between 1 and 2 is equally likely as a number between 100000 and 100001).

I only know how to generate random floats uniform w.r.t. the distinct bit patterns (i.e., each bit pattern is equally likely).

The algorithm should be uniform along a given range of the representable numbers (i.e., 4.0 to 6.0 or (as an extreme) float.min_value to float.max_value).

D.R.
  • 20,268
  • 21
  • 102
  • 205
  • Is generate_uniform_real in https://github.com/boostorg/random/blob/develop/include/boost/random/uniform_real_distribution.hpp what I'm looking for? (I need C# in the end, but I could translate that) – D.R. Sep 13 '18 at 20:35
  • Some tests show that this is probably what I want. Looking forward to "approving" answers. – D.R. Sep 13 '18 at 20:46
  • 1
    Searching for “[floating-point] random uniform” shows [a number of previous discussions](https://stackoverflow.com/search?q=%5Bfloating-point%5D+random+uniform) , including [this one](https://stackoverflow.com/questions/32360671/uniform-distribution-of-integers-using-floating-point-source). Of course, it is impossible to generate floating-point values with a continuous distribution, as the floating-point numbers are not continuous. If approximations are fine, then generating a number in [0, 1] and scaling and translating it may suffice. – Eric Postpischil Sep 13 '18 at 22:06
  • If you want something more sophisticated, such as allowing each representable number in the interval to be returned (with a probability proportional to some interval around it, either centered or one-sided, so the union of intervals of all the representable numbers equals the target interval) or requiring exactly equal probability for all potential returned numbers (thus not allowing even trivial variations due to a mismatch due to adapting a generator that returns, say 2^32 values, to a interval containing some odd number of values), then the answer is complicated. – Eric Postpischil Sep 13 '18 at 22:11

1 Answers1

0

One can translate the generate_uniform_real from https://github.com/boostorg/random/blob/develop/include/boost/random/uniform_real_distribution.hpp to create random floats uniform w.r.t the continuous number line.

D.R.
  • 20,268
  • 21
  • 102
  • 205