Article
Programming a simple simulation
A basketball player scores 70 out of every 100 free throws. How many will she score in a 20-shot game? Nobody can say for sure. But a program can play that game thousands of times in a second, and show you what usually happens. In this article you build that program, one small piece at a time.
One shot, then a whole game
In the last lesson you made something happen with a given chance. RANDOM(1, 100) picks one of 100 numbers, each equally likely. The numbers 1 to 70 are 70 of those 100, so the test RANDOM(1, 100) ≤ 70 is true 70% of the time. That test is one shot.
A game is many shots. So you need three things:
- a loop to repeat the shot,
RANDOMto decide each shot,- a counter (a variable that starts at 0 and goes up by 1 when something happens) to count the baskets.
made ← 0
REPEAT 20 TIMES
{
IF(RANDOM(1, 100) ≤ 70)
{
made ← made + 1
}
}
DISPLAY(made)The exam writes RANDOM(a, b). Python has no RANDOM, but random.randint(a, b) does the same job, with both ends included. Run the Python version several times:
You get a different answer each time, because the shots are random. The answer is always a whole number from 0 to 20 (0 baskets is very unlikely, but possible). Most runs give a number near 14, because 20 shots × 70% = 14. That is the expected value: the average you would get if you could play the game over and over.
To trace a program that uses RANDOM, you need the numbers it returned. Suppose a 4-shot game gets 45, 82, 70 and 13:
| Shot | RANDOM(1, 100) | ≤ 70? | made after |
|---|---|---|---|
| 1 | 45 | true | 1 |
| 2 | 82 | false | 1 |
| 3 | 70 | true | 2 |
| 4 | 13 | true | 3 |
Look at shot 3. Because the test is ≤, a 70 counts as a basket. With < there would be only 69 winning numbers, and the chance would be 69%.
Check your understanding
A program makes 8 attempts. Each attempt wins with a 25% chance. Which of the following could be displayed?
wins ← 0
REPEAT 8 TIMES
{
IF(RANDOM(1, 100) ≤ 25)
{
wins ← wins + 1
}
}
DISPLAY(wins)Many games, and a list of results
One game tells you little. Play many games and keep every score in a list, so you can look at all of them. The counter must be reset to 0 at the start of each game, so made ← 0 goes inside the outer loop.
results ← []
REPEAT 5 TIMES
{
made ← 0
REPEAT 20 TIMES
{
IF(RANDOM(1, 100) ≤ 70)
{
made ← made + 1
}
}
APPEND(results, made)
}
total ← 0
FOR EACH r IN results
{
total ← total + r
}
DISPLAY(total / LENGTH(results))The inner loop plays one game. The outer loop plays 5 games and adds each score to results. The last part adds the scores and divides by how many there are, which is the average. Here is the Python version. Change games to 100 and run it again, then to 10000:
(print(results) shows the whole list. AP has no way to display a list at once, so AP code displays elements one at a time.)
More runs give a steadier estimate
Each average is only an estimate of the expected value, 14. With few games it can be off by quite a lot. With many games the lucky and unlucky games cancel out, so the estimate settles down near 14.
| Games played | The average is usually between |
|---|---|
| 5 | 12 and 16 |
| 100 | 13.6 and 14.4 |
| 10,000 | 13.9 and 14.1 |
The average of 10,000 games is still not exactly 14, and it is different each time you run it. More runs make the estimate steadier, not perfect. One run of a simulation is never proof of anything.
Worked example: two dice
How often do two dice add up to 7? Roll two dice 600 times and count.
count ← 0
REPEAT 600 TIMES
{
total ← RANDOM(1, 6) + RANDOM(1, 6)
IF(total = 7)
{
count ← count + 1
}
}
DISPLAY(count)Each RANDOM(1, 6) is a separate call, so it is a separate die. The sum of two dice is 7 in 6 of the 36 equally likely pairs: 1+6, 2+5, 3+4, 4+3, 5+2, 6+1. That is 1 in 6, so the expected value is 600 × 1/6 = 100. The program can display any whole number from 0 to 600, but it usually shows a number from about 82 to 118.
The second line should be near 0.167 (which is 1/6). Look at the other ways to write two dice:
| Code | Is it two dice? |
|---|---|
RANDOM(1, 6) + RANDOM(1, 6) | Yes. Two separate rolls, added |
RANDOM(2, 12) | No. It makes 7 as likely as 2 (1 in 11), but two dice make 7 six times as often as 2 |
2 * RANDOM(1, 6) | No. One roll, doubled: only the even numbers 2, 4, … 12 |
Check your understanding
A simulation needs the total of two six-sided dice. Which expression models it?
Common mistakes
- Starting the counter inside the loop.
made ← 0inside the shot loop resets the count on every shot. Put it before the loop that it counts. - Forgetting to reset it for each game. For a list of game scores,
made ← 0must be inside the games loop, but outside the shots loop. - Using
<when you mean≤.RANDOM(1, 100) < 70is a 69% chance. - Expecting the expected value exactly. An expected value of 100 does not mean every run shows 100.
- Trusting a few runs. Five games are a poor estimate. A thousand games are a much better one.
- Adding two dice as
RANDOM(2, 12). The totals are not equally likely.
On the exam
You may get a simulation code segment and be asked "Which of the following could be displayed?" Work out the smallest and the largest possible value from the counter and the number of passes. Any whole number in that range is possible, even an unlikely one. You may also be asked what a variable is for ("it counts the baskets"), or which statement about the results is true. Remember that more runs give a steadier estimate, but never an exact one.
Key words
Simulation: a program that imitates a real situation using random numbers.
Counter: a variable that starts at 0 and goes up by 1 each time something happens.
Expected value: the average result over a very large number of runs, such as shots × chance.
Estimate: a result from a simulation that is close to the true value, but not exactly it.
Run: one go at the whole simulation, from start to finish.