Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python random.random - chance of rolling 0

Tags:

python

random

As described in the documentation, random.random will "return the next random floating point number in the range [0.0, 1.0)"

So what is the chance of it returning a 0?

like image 711
rovyko Avatar asked Jan 06 '23 08:01

rovyko


1 Answers

As per the documentation, it

It produces 53-bit precision

And the Mersenne Twister it is based on has a huge state space, many times large than this. It also routinely passes statistical tests of bit independence (in programs designed to spot patterns in RNG output). The distribution is essentially uniform with equal probability that any bit will be 0 or 1.

The probability of getting precisely 0.0 will be 1 in 2^53 (assuming an unknown internal state)

like image 88
Neil Slater Avatar answered Jan 12 '23 03:01

Neil Slater