Search code examples
pythonmathrandom

Why is randint seemingly not uniformly distributed?


Consider the code:

from random import randint
from math import log10

print(log10(sum([randint(0, 1e12) for _ in range(int(1e6))]) / 1e6))

I believe the result should be close to 11, but instead the output is: 11.699144269464252

Some context:

root@debian:~# python3 --version
Python 3.11.2
root@debian:~# uname -a
Linux debian 6.1.0-20-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.85-1 (2024-04-11) x86_64 GNU/Linux

Solution

  • The expected value for sum([randint(0, 1e12) for _ in range(n)]) / n) is 1e12 / 2 if randint is uniformly distributed and n is large.

    So the expected result should be log10(1e12 / 2) which is 11.698970004336019, which is "pretty close"1 to what you got.

    So the result you got does not indicate that randint is not uniformly distributed.


    1What "close" even means is a statistics/probability question and is out of scope for this site.