|
|
|
|
|
by raymondh
1533 days ago
|
|
The Python docs¹ include an implementation of Allen Downey's algorithm: from random import Random
from math import ldexp
class FullRandom(Random):
def random(self):
mantissa = 0x10_0000_0000_0000 | self.getrandbits(52)
exponent = -53
x = 0
while not x:
x = self.getrandbits(32)
exponent += x.bit_length() - 32
return ldexp(mantissa, exponent)
¹ https://docs.python.org/3/library/random.html#recipes |
|
https://i.imgur.com/5TerZkm.png
Top is Python, bottom is correctly rounding to nearest float. Python's implementation does not round correctly to the nearest floating point value, it floors. This means it oversamples those floating point numbers with mantissa 0, and can not sample an inclusive range at all.