Home Academia Contact Archive

PaC Exercise 2.24

Posted on February 18, 2021
import numpy as np

Exercise 2.24

We roll a standard fair die over and over. What is the expected number of rolls until the first pair of consecutive sixes appear?

Solution: Let X be number of throws until we see two consecutive sixes and let Y be the outcome of the first throw. First, we conclude that


$$ \begin{align*} E[X | Y=6] &= \sum_{i=2}^\infty i P(X=i | Y=6) = 2 \frac{1}{6} + \sum_{i=3}^\infty i P(X=i | Y=6) = \frac{2}{6} + \frac{5}{6}(2 + E[X]),\\ E[X | Y\neq 6] &= 1 + E[X]. \end{align*} $$

Then,


$$ E[X] = \sum_{y=1}^6 P(Y=y) E[X|Y=y] = \frac 5 6 (1 + E[X]) + \frac 1 6 \left(\frac{2}{6} + \frac{5}{6}(2 + E[X])\right) = \frac{35}{36} E[X] + \frac{42}{36}, $$

and thus


E[X] = 42.

We verify this result by simple sampling.

num_samples = 10**7

samples = np.random.randint(low=1, high=7, size=num_samples)
consecutive_sixes = np.array([6,6])

i = 0
lengths = []
while i < len(samples) - 1:
    if np.array_equal(samples[i:i+2], consecutive_sixes):
        lengths.append(i)
        i += 1
    i += 1
    
np.diff(lengths).mean()
41.89661558056326