Random Variables · Geometric Distribution

Lesson 6

Nikolai Chukhin · Alexander S. Kulikov

Thus, if we roll a die and wait until a six appears, then on average, we will have to wait for six rolls. And waiting for the first heads (on a fair coin) on average will take two flips. There is a natural explanation for this. Suppose we roll a die \(n\) times and record the results.

from random import randint, seed

seed(17)

for _ in range(28):
    x = randint(1, 6)
    print(x, end=' ')

5 4 3 3 3 2 6 6 5 6 3 1 1 2 4 6 4 3 5 3 6 6 6 4 2 5 1 2
A six will appear approximately \(n/6\) times. Now we divide this sequence into segments ending with sixes.
from random import randint, seed

seed(17)

for _ in range(28):
    x = randint(1, 6)
    print(x, end=' ' if x != 6 else '\n')

5 4 3 3 3 2 6
6
5 6
3 1 1 2 4 6
4 3 5 3 6
6
6
4 2 5 1 2
The average number of repetitions up to and including the first success is the average length of all these segments. But the total number of these segments is approximately \(n/6\), and their total length is \(n\). Hence, the average segment length is six.

Let's illustrate this with an experiment for \(n=10^{5}\).

from random import randint, seed
from statistics import mean

seed(17)

values = []
for _ in range(10 ** 5):
    num_steps = 1
    while randint(1, 6) != 6:
        num_steps += 1
    values.append(num_steps)

print(mean(values))

6.03217