Random () in python

In python, the random() function generates a random float evenly in the half-open range [0.0, 1.0]. Basically, can it ever generate 0.0 (i.e. Zero) and 1.0 (i.e. Unity)? What is the practicality scenario?

+4
source share
2 answers

0.0 can be generated; 1.0 cannot (since it is not within the range, therefore ) unlike [ ).

The probability of generating 0.0 is equal to the probability of generating any other number in this range, namely: 1 / X, where X is the number of different possible results. For a standard double-precision unsigned floating point, this usually means 53 bits of the fractional component, for 2 ^ 53 possible combinations, which leads to a probability of 1 / (2 ^ 53) generation exactly 0.0 .

So, although it is possible that it will return exactly 0.0 , it is unlikely that you will see this soon, but it is unlikely that you will see exactly any other value that you could choose in advance.

+13
source

[ indicates that 0.0 included in the range of permissible outputs. ) indicates that 1.0 not in the range of valid outputs.

+11
source

Source: https://habr.com/ru/post/1306666/


All Articles