# Writing a program to see how long it takes to get HHHH when flipping a coin

Yesterday we did a fun project exploring how long it takes, on average, to create certain words like COVFEFE or ABRACADABRA when selecting letters at random. We also simplified the problem a bit by looking at sequences of H’s and T’s for coin flips. That project is here:

Talking Markov chains and Martingales with kids

Today’s project was writing a computer program to simulate flipping a coin until we saw HHHH. In yesterday’s project we found that it would take 30 coin flips on average. We started today’s project by talking about how to write a program to do this simulation. Following this discussion the boys wrote their program off camera:

When the boys finished their program we talked through it and looked at the shape of the distribution of the number of flips it took to get to HHHH. They were pretty surprised by this shape:

To wrap up the project we spent 5 min talking about how the program would need to change to look at a general sequence of 4 flips – HTHT, for example. We didn’t actually make the changes, though, as we’d already spent enough time working through the ideas this morning:

This was a fun statistics / programming project that has a pretty surprising result. We’ll definitely have to follow up with the program for a generic sequence of flips soon!