Simulations
What a simulation is, why programmers build them instead of running real experiments, what gets left out, and how leaving things out can bias the results.
1What a simulation is
A simulation is an abstraction of a more complex object or event, built for a specific purpose. It keeps the parts that matter for one question and drops the rest. It uses values that change (how many people are in line, where each car is, how much water is in a river) to represent the changing state of the thing being studied.
Simulations usually copy a real-world event so you can draw conclusions about it, without the limits of doing it for real. And because real life is full of chance, simulations use random numbers to model that variety.
2A simulation you can read
A food truck owner wonders whether one cook can keep up at lunch. Each minute, 0, 1, or 2 customers walk up (on average one a minute), and the cook serves one customer a minute. This program simulates one hour and reports the longest line:
line ← 0
longest ← 0
REPEAT 60 TIMES
{
line ← line + RANDOM(0, 2)
IF (line > 0)
{
line ← line - 1
}
IF (line > longest)
{
longest ← line
}
}
DISPLAY(longest)
lineis the changing state: the number of people waiting.RANDOM(0, 2)models the real-world variety in how many people arrive.- Everything else about the truck (the menu, prices, the weather, people's names) was left out. It doesn't matter for this question.
Each run is one possible hour, so the owner runs it 1,000 times. With the cook alone, the longest line averaged 6.9 people, and in 36% of the hours it reached 8 or more. Then the owner changed one part of the model: with a helper taking orders, the truck serves 1 or 2 people a minute (served ← RANDOM(1, 2)). Now the longest line averaged 2.4, and fewer than 1% of the hours reached 8 or more.
Arrivals and serving balance out on average, so you might guess the line stays short. The simulation shows why it doesn't: a few busy minutes in a row build a line that one cook can only work down slowly.
3Why simulate
Simulations are most useful when the real experiment is impractical:
| The real thing is… | Example | What the simulation allows |
|---|---|---|
| too big | two galaxies colliding | watch something no one can set up |
| too small | a protein folding inside a cell | see what can't be seen by eye |
| too fast | a car crash, over in a tenth of a second | slow it down and pause it |
| too slow | a forest regrowing over 200 years | speed it up to minutes |
| too expensive | testing ten bridge designs | try each design before buying steel |
| too dangerous | an illness spreading through a city | test rules without anyone getting sick |
A simulation can also be run thousands of times, with one thing changed each time, which is rarely possible with the real event.
4Simplifying, and the bias it can bring
Building a simulation means removing specific details or simplifying how things work. The food-truck model treats every customer as taking one minute and ignores the menu entirely. That is what makes it small enough to write and fast enough to run thousands of times.
But a simulation can contain bias that comes from the choices of which real-world parts were included or left out. The food-truck model assumes customers arrive at the same rate all hour. If the real truck has a rush in the first 20 minutes, the model is wrong in a way that makes the line look shorter than it will be. Adding that rush (up to 3 arrivals a minute for the first 20 minutes) raised the average longest line with a helper from 2.4 to 5.8.
Nothing in the code looks broken when this happens. The bias lives in the decisions about what to model, so the question to ask is always: what was left out, and could it change the answer?
A simulation is a simplified model, built for a purpose, that uses changing values to represent a changing situation and random numbers to model real-world variety. It lets you investigate events that are too big, small, fast, slow, expensive, or dangerous to test for real, but what its builders included or left out can bias its results.
5Testing a hypothesis
Simulations help people form and refine hypotheses (educated guesses) about the thing they model. The owner's first guess was “one cook can keep up.” The runs said no. The next guess, “a helper will keep the line short,” held up, until the rush-hour version showed it might not at noon. Each round sharpens the question.
6Worked examples
Free throws
A player makes 70% of her free throws. This simulates one game in which she shoots 10.
made ← 0
REPEAT 10 TIMES
{
IF (RANDOM(1, 10) ≤ 7)
{
made ← made + 1
}
}
DISPLAY(made)
What can it display, and why does it display something different each run?
- 1
RANDOM(1, 10) ≤ 7is true for 7 of 10 equally likely values, so each shot goes in 70% of the time. - 2
madecounts the shots that went in, so it can be any whole number from 0 to 10. - 3Four runs displayed 6, 8, 4, 9. Each call to
RANDOMcan return a new value, just as real shots vary. - 4In 2,000 runs, she made 8 or more in 37% of the games. Probability math gives 38%, so the simulation agrees.
When is a simulation the better choice?
For which task is a simulation more useful than doing it for real: (a) counting the students in a classroom; (b) predicting how a wildfire spreads under different winds; (c) timing how long one student takes to walk to the gym?
- 1(a) and (c) are quick, safe, and cheap to measure directly.
- 2(b) is too dangerous (and too big) to test for real, and you want to try many wind conditions.
The flood model
A city simulates flooding from heavy rain. The model includes rainfall, rivers, and hills, but leaves out the storm drains that carry water away. How might this bias the results?
- 1Bias comes from the real-world parts that were included or left out.
- 2Storm drains remove water, so leaving them out makes the simulated water rise higher than it would.
Which detail can go?
A traffic simulation studies how long cars wait at a light. Which detail can be removed without hurting its purpose: (a) how long the light stays green; (b) the color of each car; (c) how many cars arrive per minute?
- 1(a) and (c) directly decide how long cars wait.
- 2A car's color has no effect on waiting time.
7Common mistakes
A simulation with random numbers gives a different result each run. Run it many times and look at averages or how often something happens.
A simulation is only as good as its simplifications. Results are evidence, not proof, and they should be checked against the real world when possible.
Bias in a simulation usually comes from what was included or left out, not from a typo. Ask which real-world parts are missing.
If the real thing is quick, safe, and cheap to observe, observe it. Simulations earn their place when reality is impractical.
“The program simulates the food truck's line by tracking how many people are waiting, using RANDOM(0, 2) to model how many customers arrive each minute. Running it many times shows how long the line usually gets, without testing it on real customers. Because it assumes customers arrive at a steady rate, it may understate the line during a lunch rush.”
Say what it models, which values change, where randomness comes in, why it beats the real experiment, and what it leaves out.