Expected Tosses for Three Consecutive Heads

A fair coin is tossed repeatedly until three consecutive tosses are heads. What is the expected number of coin tosses?

Solution:

Let x be the expected number of tosses until 3 consecutive heads occur.

The image below shows the possible scenarios that arise when starting tossing:

  • You could have a perfect HHH streak, and you would just end the game in 3 tosses.
  • You could roll a tail on the first try, so you would have to *start from scratch,*with 1 toss already made
  • You could roll a head, then a tail, after which you would have to *start from scratch,*with 2 tosses already made
  • You could roll two heads, then a tail, after which you would have to *start from scratch,*with 2 tosses already made

probability tree

This in turn can be converted to a mathematical equation:

x=12(x+1)+14(x+2)+18(x+3)+183x = \dfrac{1}{2} \cdot (x+1) + \dfrac{1}{4} \cdot (x+2) + \dfrac{1}{8} \cdot (x+3) + \dfrac{1}{8} \cdot 3

Multiplying by 8, this becomes: 8x=4x+4+2x+4+x+3+38x = 4x+4+2x+4+x+3+3

Further simplifying the equation, we get:8x=7x+148x = 7x + 14

So, thus, we finally arrive at our final result:x=14x = 14

Checking our solution via code:

When possible, it’s always a good idea to check your maths with a quick simulation.

We will use python and two very common libraries: NumPy and random. We simulate 1 million games, using a while loop inside a simple “for loop”. Our for loop allows us to get through the games, while the while checks if our condition is met. In the end, we just do a mean of our results and check how it compares with our mathematical result.

import numpy as np import random required_heads = 3 length_array = [] for i in range(int(1e6)): heads = 0 length = 0 while heads<required_heads: length += 1 if random.choice(['H','T'])=='H': heads += 1 else: heads = 0 length_array.append(length) print(f"Average number of heads needed: {np.mean(length_array)}")
Average heads needed: 13.997503