In this lesson, students will gain more practice using loops as we develop a simulation that repeatedly flips coins until certain conditions are met. The lesson begins with an unplugged activity in which students flip a coin until they get 5 heads in total, and then again until they get 3 heads in a row. They will then compete to predict the highest outcome in the class for each statistic. This activity motivates the programming component of the lesson in which we develop a program that allows them to simulate this experiment for higher numbers of heads and longer streaks.
Students will be able to:
The ability to model and simulate real-world phenomena on a computer has changed countless fields. Researchers use simulations to predict the weather, the stock market, or the next viral outbreak. Scientists from all disciplines increasingly rely on computer simulation, rather than real-life experiments, to rapidly test their hypotheses in simulated environments. Programmers might simulate users moving across their sites to ensure we can handle spikes in traffic, and of course video-game and virtual reality technology is built around the ability to simulate some aspects of the real world. The speed and scale at which simulations allow ideas to be tested and refined has had far-reaching impact, and it will only continue to grow in importance as computing power and computational models improve.
For this lesson, students will begin by participating in the following unplugged activity. First, have them flip a coin until they get 5 total heads. Then have them flip a coin again until they get 3 heads in a row. Have students record their results in the "Flipping Coins Worksheet" resource and predict the highest result in the class. Use the following paragraph as a prompt for them.
We're going to run two simple experiments. Use your worksheets to keep track of your results (by writing "H" or "T" for each flip) but keep them a secret for now.
That was pretty interesting with only 5 total heads or a streak of 3 heads. If we want to run this experiments for higher numbers of heads or longer streaks of heads however, we'll quickly find that it's tedious to do once, let alone many times. Luckily we know now that we can use loops to repeatedly perform commands, so we're going to simulate these larger experiments instead.
The series of problems and tasks in this lesson progressively build up a series of experiments that simulate coin flipping.
Students are asked to make a hypothesis, then experiment with code, revise the hypothesis and so on, around some question. Below are some guidelines about what students should find.
This lesson might seem to naturally lend itself to a more detailed discussion of the mathematical properties of random experiments. Flipping coins is, after all, perhaps the most classic example of a random experiment.
The goal of this lesson is not for students to walk away knowing the precise mathematical relationship between the number of coins flipped and the number of heads observed or the longest streak of heads. Instead they are supposed to appreciate that questions that might be impossible or hard to address by hand are possible to examine by using computer simulation. Just as developing a mathematical model is one way to address a problem, so too is developing a simulation.
There's no need to dive deep into the mathematics this lesson touches on, but students should be able to describe the patterns we observed while running our simulations, and use those observations to justify new hypotheses
The remainder of this Lesson's Activity section is written in the form of instructions for students to follow and learn coding concepts. This also gives students the online IDE to code within.
Over the next several exercises we are going to be building a program that simulates flipping coins in order to find out how many flips it takes to get 10,000 heads in total and how many flips it takes to get a streak of 12 heads in a row. If you're following along on the worksheet, you might have already tried these experiments at a smaller scale, flipping a coin until you got 5 total heads and a streak of 3 heads in a row. Doing this smaller scale activity may have given you some intuition for what the outcome of the larger experiment will be, but before running an experiment it's usually a good idea to develop a hypothesis.
What will be the maximum and minimum number of flips it will take to get 10,000 heads? When trying to get 5 heads some groups may have taken exactly 5 flips while others took 15 or more (3 times the number of heads you were going for!). Do you think this wide range of values will persist when trying for 10,000 heads?
What will be the maximum and minimum number of flips it will take to get a streak of 12 heads? Again think about the patterns you've observed. If you need 4 times as many heads in a row will it take 4 times as long? Try to justify the values you choose.
Write down your predictions on the worksheet so you can reference them later. It's not important whether you're right (you'll have the answer soon enough anyway!) but it will let you reflect on your thinking. In fact, you might even get some new ideas you'd like to investigate as a result!
To start, we're going to simulate flipping a coin 10 times. You might be thinking that isn't many coin flips, and that we could just do those flips in real life, but this is actually an important step in developing a simulation. At small scales we can make sure our code is working as intended because we can still confirm its output. Once we're convinced that the logic of our program is reliable we'll move up to simulating larger numbers of flips.
The core logic of our program will be focused on a "repeat while" loop that simulates flipping a coin by repeatedly generating random 0's or 1's using randomNumber. This is a great opportunity to keep practicing using loops while applying your knowledge of variables, iteration, and if statements.
Let's say that a 1 is a heads. If we want our simulation to run until we reach a certain number of heads, we should first start by counting the number of heads that have been flipped. In order to do this you will need to add a variable that counts the number of heads. Initialize it to 0, and every time you flip a heads (1) increment this heads counter by 1. To ensure your program works correctly, output the current coinFlip, number of heads, and number of flips at the end of each iteration of the loop.
Currently our loop condition is based on a counter variable that keeps track of the total number of flips, but our simulation should only run while we have fewer than the target number of heads. In this exercise you are going to change the condition used by your "repeat while" loop so that your simulation terminates once you have flipped 5 total heads. At the end of the loop we will write the total number of flips to know how many flips it took to get 5 heads.
You're ready to increase the number of heads your simulation is going for and test your first hypothesis. Before we move up to the full 10,000 heads, however, we're going to perform a quick check of our program logic. When you make changes to your program it is possible that some portion of your programming logic will stop working as you expected. In order to feel more confident about your model you will first change the number of heads you are going for to a number that we can still verify (7 heads). If our code still works after making changes then we should be confident that it should work at 10,000. We will remove the intermediate output and run the full simulation!
If everything has gone well you should already be able to make a determination about your first hypothesis.
Take some time to write out your thoughts and whether your understanding of this system has changed. Update either of your hypotheses to reflect what you observed in this first experiment.
We are going to alter our simulation so that it doesn't count the total number of heads, but rather the longest streaks of heads. This will allow us to simulate how many flips it takes to get 12 heads in a row.
To begin with you will change your looping condition so that the loop again only runs 10 times. This will allow us to confirm our code is working.
We know we need to count streaks of heads, but how do we do this in code? Do we need to keep track of all the previous flips so we know that we're on a streak?
The answer is: no. We can instead just count in a clever way that makes our code pretty simple. Make a variable to use as a counter and...
Here is some psuedocode showing how it works. You might take a minute to study and reason about why and how it works.
// Outside loop headsCount = 0 // Inside loop IF (current flip is a heads) headsCount = headsCount + 1 ELSE headsCount = 0 END DISPLAY (current flip) DISPLAY (headsCount)
The example below shows an example of what output from your program might be.
Note: It is possible that you don't get any streaks of heads, due to using random numbers. As such, you may need to run your program several times to test.
0 Heads Streak: 0 1 Heads Streak: 1 1 Heads Streak: 2 0 Heads Streak:0 0 Heads Streak:0 1 Heads Streak: 1 1 Heads Streak: 2 1 Heads Streak: 3 0 Heads Streak: 0 1 Heads Streak: 1
We want our simulation to run while the streak of heads is less than a target length. In order to do this we'll need to change our looping condition to use the variables we've been using to count our streak of heads. To begin with your simulation should try for a streak of 3 heads so that you can still confirm the output.
The example below shows an example of what output from your program might be.
1 Heads Streak: 1 Flip Count: 1 0 Heads Streak: 0 Flip Count: 2 0 Heads Streak: 0 Flip Count: 3 0 Heads Streak: 0 Flip Count: 4 0 Heads Streak: 0 Flip Count: 5 1 Heads Streak: 1 Flip Count: 6 1 Heads Streak: 2 Flip Count: 7 1 Heads Streak: 3 Flip Count: 8
We're almost ready to test our second hypothesis and find out how long it takes to get 12 heads in a row. Just as before we're first going to test our code with a different length streak (4) before removing most of the visual output and running the code for a streak of 12.
You should now be ready to compare the results of your simulation to your second hypothesis.
Just as before, you should update your hypotheses to reflect the results of your simulation. Try to come up with a reasonable explanation for the results you observed.
Present the following prompt to students after they've finished the Activity.
Update your hypothesis based on the results of your simulation and predict the outcomes of an even larger experiment using the new knowledge you have gained.
Present the following conclusions to students in an ending discussion.
Not all problems are as easy to simulate as a coin flip of course, and we've even encountered how some of the problems we can simulate still take a very long time to run.
Simulations are an increasingly important tool for a variety of disciplines. Weather and traffic predictions are based on computer models that simulate weather patterns or people moving through a city. Scientific research, whether in physics, chemistry, or biology, increasingly use simulations to develop new hypotheses and test ideas before spending the time and money to run a live experiment.
Before you use most of your favorite websites and apps, they will be tested by simulating high levels of traffic moving across the server. Simulations take advantage of computers' amazing speed and ability to run repeated tasks, as we've encountered through our exploration of the while loop, in order to help us learn more about the world around us.
As computers get ever faster and models improve, we are able to answer old questions more quickly and start asking new ones.
Extend this activity to new statistics, rolling dice, etc. Use these both as opportunities to practice programming and to develop the habit of using simulations to refine hypotheses.