Simulations & Randomness
Want to know what happens if you flip a coin a thousand times? You could spend an afternoon flipping — or let a computer do it in a blink.
Swipe or tap Next for the next section
Trying Things Without Doing Them
A simulation is a program that imitates something from the real world, so you can explore what might happen without actually doing it. Simulations are used to:
- predict the weather and plan for storms,
- test how traffic would flow with a new road or traffic light,
- train pilots without risking a real plane,
- model how a disease might spread, and what slows it down.
Simulations let us test ideas that would be too slow, too costly, or too dangerous to try for real.
Randomness in Code
Real life is full of chance, so many simulations use random numbers. In pseudocode, RANDOM(a, b) gives a random whole number from a to b, including both ends:
roll ← RANDOM(1, 6)
IF (roll = 6)
{
DISPLAY("You win!")
}Each run of that code has a 1 in 6 chance of displaying "You win!". Run it once and anything can happen. Run it thousands of times and patterns appear — try it:
The dashed line on each bar shows the share you'd expect in the long run.
What Simulations Leave Out
Every simulation is an abstraction: it keeps the details that seem to matter and leaves the rest out. A traffic simulation might treat every driver the same, or ignore rain. That makes it possible to build — but it also means the results are only as good as those choices.
- Wrong or missing assumptions lead to wrong predictions.
- A simulation can be biased by the data it's built from.
- Results should be checked against the real world whenever possible.
A simulation is a useful guess, not a guarantee.
5 quick questions. Pick one answer for each — you'll see why straight away.
Why might a city use a traffic simulation before building a new road?
A simulation lets you try many "what if" ideas safely and cheaply. It won't be perfect, but it helps compare options.
Which values could RANDOM(1, 6) give?
RANDOM(a, b) gives a random whole number from a to b, including both ends.
You flip a coin 10 times in a simulation and get 7 heads. What's the best explanation?
Short runs are often lopsided. Over thousands of flips, the share of heads settles near 50%.
A simulation of a disease spreading leaves out that some people stay home when sick. What does that mean?
Every simulation simplifies the real world. Leaving out details makes it faster to build, but can change the answer.
When you roll two dice and add them, which total comes up most often?
More combinations make 7 (1+6, 2+5, 3+4, 4+3, 5+2, 6+1) than any other total. 2 and 12 have only one way each.