Skip to main content

Section 8.10 Coin Flips

Now that we know how to generate random numbers, let us try to do a simulation of coin flips.
If we flip a coin, we expect to get heads about half the time and tails about half the time. Sometimes we will get a few heads or tails in a row. But longer streaks of all heads or tails are less likely. It is twice as hard to get 3 heads in a row as to get 2. And twice as hard again to get 4 heads in a row. So let’s flip some coins and see how long it takes to get 4 heads in a row.
To simulate flipping a coin, we need to map the possibilities to different numbers. Let’s call tails 0 and heads 1. Flipping a coin is then the same as picking either 0 or 1, which we can do with randRange(0, 1). Any time we want to pick something at random, we can apply the same basic strategy. Say we want to pick a random day of the week. There are seven days, so we could use randRange(0, 6) or randRange(1, 7) and then map each possible number to a different day of the week (i.e. 0 means "Sunday", 1 means "Monday", etc...)
Let’s start with a simple test of this idea. We will make a loop that counts until 10. In the loop we will generate a random number and use that to print "Heads" or "Tails":
Listing 8.10.1.
Now we need to keep track of how many heads in a row we have seen. We will need a variable to store the current streak. We will start it at 0. Every time we see a head, we will increment the streak. Every time we see a tail, we will reset the streak to 0. The variable will need to be initialized once before the start of the loop - we do not want to initialize it inside the loop or it will reset with each flip.

Checkpoint 8.10.1.

Fix the missing parts of the code below. They are marked with ???. To make sure we see some longer streaks, we are now flipping 100 coins - you will need to scroll the output area to see all of it.
Once we are satisfied that works, we can worry about our real goal. We want to keep flipping until we get 4 heads in a row and then stop. We could add something like this at the end of our loop body:
if (headStreak == 4) {
    break;
}
But there is no guarantee we will see 4 heads in the first 100 flips. We could make the loop count to 1000, but it is still possible (although very unlikely) we wouldn’t see 4 heads.
The real problem here is that our problem involves counting, but the loop we need is not a counting loop We need a sentinel value loop - one that stops when headStreak is 4. Our counter is something extra we are doing, not what drives the loop logic.

Checkpoint 8.10.2.

Build a version of the simulation that correctly runs until 4 heads in a row are seen.
You have attempted of activities on this page.