What does "depending on rounding" mean?

About random.uniform , docstring says:

Get a random number in the range [a, b) or [a, b] depending on rounding.

But I don’t know what exactly means "depending on rounding."

+6
source share
2 answers

The current documentation for random.uniform() reads:

Returns a random floating-point N such that a <= N <= b for a <= b and b <= N <= a for b < a .

The final value of b may or may not be included in the range, depending on the rounding of the floating point in the equation a + (ba) * random() .

Computer floating-point arithmetic is limited in accuracy and rounding errors caused by this inaccuracy can cause b not be included in the entire range of values ​​used for the random value returned by uniform() .

random.random() returns a value between 0.0 (inclusive) and 1.0 (exception). There are values ​​for a and b where the calculation of the floating point sum of the sum a + (ba) * (1.0 - epsilon/2) not equal to b , but will be one minute less than b , while for other combinations the sum is b . epsilon is the minimum precision of the floating point number on your platform (see sys.float_info ) and 1.0 - epsilon/2 maximum value of random.random() can return.

If you are interested in information about why floating point arithmetic on computers is inaccurate, I can recommend the following two articles:

+7
source

rounding : if I round 3.45 up, I get 4. but if I round it down I get 3. so that in this case the values ​​of the ranges coming from [or] will change depending on the round

-1
source

All Articles