About random.uniform
, docstring says:
Get a random number in the range [a, b) or [a, b] depending on rounding.
But I do not know what does 'depending on rounding' exactly mean.
The current documentation for random.uniform()
reads:
Return a random floating point number
N
such thata <= N <= b
fora <= b
andb <= N <= a
forb < a
.The end-point value
b
may or may not be included in the range depending on floating-point rounding in the equationa + (b-a) * random()
.
Floating point arithmetic on computers is limited in precision and rounding errors caused by this imprecision may lead to b
not being included in the full range of values used for the random value that uniform()
returns.
random.random()
returns a value between 0.0 (inclusive) and 1.0 (exclusive). There are values for a
and b
where a floating point calculation of the sum a + (b-a) * (1.0 - epsilon/2)
does not equal b
, but will be a minute amount lower than b
, while for other combinations the sum does equal to b
. epsilon
is the minimum precision of a floating point number on your platform (see sys.float_info
), and 1.0 - epsilon/2
the maximum value random.random()
can return.
If you are interested in the details of why floating point arithmetic on computers is imprecise, I can recommend the following two articles:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With