AP® Computer Science Principles review sheet from Aim for Five (aimforfive.com/csp/units/3/3-16)
Unit 3 · Topic 3.16
3.16 Simulations
A simulation is a program that imitates something in the real world so you can study it. This topic covers why simulations are useful, how they simplify reality (and can pick up bias doing so), and how random numbers model real-world variety.
Key terms
- simulation
- model
- abstraction
- random values
- bias in simulations
What a simulation is
A simulation is an abstraction of a more complex object or event, built for a specific purpose. It uses changing sets of values to represent how the thing it models changes over time. Examples include weather forecasts, traffic models, disease-spread models, flight simulators and video games with realistic physics.
Simulations let you draw conclusions about a real situation without the limits of the real world. You can run a year of traffic in seconds, or test a bridge design in an earthquake without building the bridge.
When simulations are most useful
They shine when trying the real thing isn't practical, because it's huge or tiny, happens too quickly or too slowly, or would cost too much or put people at risk.
- Too big or slow: how a city's population might change over 50 years.
- Too small or fast: how molecules move in a gas.
- Too expensive: testing a hundred rocket designs.
- Too dangerous: how a wildfire spreads through a town.
Simplifying reality
Building a simulation means removing details and simplifying how things work. A traffic simulation might treat every car as the same size and every driver as equally careful. That makes it possible to build and run, but it also means results are only as good as those choices.
Simulations can contain bias from what the designers chose to include or leave out. A model of school pick-up traffic that leaves out students who walk or bike might wrongly conclude the school needs a bigger parking lot.
Simulations help people form a hypothesis and refine it: run the model, compare with real data, adjust, and run it again. Comparing a simulation's results with what really happens is how you judge whether it's trustworthy.
Randomness models variety
Real events vary. Not every free throw goes in, and not every customer arrives at the same time. Random number generators let a simulation copy that variety. Running a simulation many times shows the range of likely results, not just one outcome.
made ← 0
REPEAT 100 TIMES
{
IF (RANDOM(1, 10) ≤ 7)
{
made ← made + 1
}
}
DISPLAY(made)
This models 100 free throws by a player who makes 70% of them. Each run displays a different count, usually somewhere near 70.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Reading a simulation
In the free-throw simulation above, what is the smallest and largest value it could display, and what value would you expect it to be near? Why can't you know the exact output in advance?
Show the solutionHide the solution
- Step 1: Each of the 100 passes adds 1 when RANDOM(1, 10) is 1 to 7, which happens 7 times out of 10.
- Step 2: If every random value were 8 or more, made would stay 0. If every value were 7 or less, made would be 100. So the output is between 0 and 100.
- Step 3: On average 70% of passes add 1, so the result is usually near 70.
- Step 4: The values come from RANDOM, so each run can be different.
Answer: Anywhere from 0 to 100, usually near 70; the exact value changes from run to run because it depends on random values.
- Example 2
Spotting a limitation
A city simulates bus ridership using each route's average daily riders. The simulation assumes the same number of riders every day of the year. Give one way this simplification could lead to a wrong conclusion.
Show the solutionHide the solution
- Step 1: Name what was left out: real ridership changes with weather, school days, holidays and events.
- Step 2: Explain the effect: on rainy school days buses may be packed, so a model built on averages could conclude there are enough buses when riders are actually left waiting at peak times.
Answer: By ignoring day-to-day changes like school days and bad weather, it could underestimate crowding at busy times and suggest fewer buses than are needed.
Common mistakes
- Thinking a simulation's results are always accurate. Every simulation leaves things out, and what's left out can bias the results.
- Saying simulations remove the need for real-world data. Real data is how you check and improve a model.
- Expecting a simulation with random values to give the same output every run.
On the exam
- Expect questions on the benefits of a simulation in a scenario (speed, cost, safety) and on its limitations (details left out, possible bias).
- Some questions show simulation code with RANDOM and ask what it models or what range of outputs is possible.
Connected topics
Videos
Check yourself
3 questions on 3.16 Simulations. Pick an answer to see if you got it, and why.
A city wants to study how a wildfire might spread through nearby hills under different wind conditions. Which is the best reason to use a computer simulation for this?
A simulation of traffic at a downtown intersection models only cars and leaves out buses, bikes and pedestrians to keep the program simple. Which is the most likely result of this choice?
heads ← 0
REPEAT 100 TIMES
{
IF (RANDOM(1, 2) = 1)
{
heads ← heads + 1
}
}
DISPLAY(heads)The programmer wants to use the simulation to estimate the fraction of flips that land heads. Which change would most likely make the estimate more reliable?
0 of 3 answered