Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'depending on rounding' exactly mean?

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.

like image 361
user805627 Avatar asked Nov 03 '12 21:11

user805627


1 Answers

The current documentation for random.uniform() reads:

Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.

The end-point value b may or may not be included in the range depending on floating-point rounding in the equation a + (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:

  • The Floating Point Guide
  • What Every Computer Scientist Should Know About Floating-Point Arithmetic
like image 67
Martijn Pieters Avatar answered Sep 28 '22 00:09

Martijn Pieters