Topic 3.16: Simulations | AP CSP Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 3.16 Simulations
3.16
Big Idea 3 • Algorithms & Programming

Simulations

🕐 ~40 min FREE 📖 6 MCQ questions 🎮 Simulation Station game 💻 Live Python editor AAP-3.F

After this lesson, you will be able to:

  • Explain what a simulation is and how it abstracts a real-world phenomenon using assumptions
  • Give concrete reasons a simulation is used instead of a real experiment, such as cost, safety, speed, and repeatability
  • Describe how randomness and iteration let a simulation run many trials and tally outcomes
  • Explain why more trials generally give a more reliable estimate while results stay approximate
  • Identify the limitations of a simulation, including its dependence on the model and its assumptions
  • Read and trace simulation code that seeds randomness, loops over trials, and reports a summary
📈 Exam weight: Simulations (AAP-3.F) show up as reasoning questions across Big Idea 3: why simulate, what randomness and loops are doing, and what results can and cannot prove. The benefit-versus-limitation trade-off is a favorite trap, and a simulation is a natural Create Task program.
💡 Think about this first

How would you find the chance of rolling a 6? You could roll a real die thousands of times, or you could write ten lines of code that rolls a virtual die a million times in a second, counts the sixes, and reports the fraction. That is a simulation: a safe, fast, repeatable model of the real world. The catch is that a model is only a model, and this lesson is about trusting it exactly as far as its assumptions allow.

What a Simulation Is

A simulation is a program that imitates a real-world process so you can study it on a computer instead of in the real world. It is a kind of abstraction: it keeps the details that matter for the question you are asking and leaves out everything else. A flight simulator does not recreate every molecule of air; it keeps just enough physics to practice flying. Because a simulation is built on assumptions and hypotheses about how the real thing behaves, it is a simplified stand-in, not a perfect copy.

Many simulations include randomness, because the real process being modeled has chance in it: a coin flip, a dice roll, whether it rains, which customer arrives next. The simulation uses a random number generator to mimic that uncertainty, and it uses a loop to run the scenario many times so it can see the range of things that could happen.

import random
random.seed(1)
heads = 0
for i in range(100):
    if random.randint(0, 1) == 1:
        heads = heads + 1
print(heads)
heads <- 0
REPEAT 100 TIMES
{
    flip <- RANDOM(0, 1)
    IF (flip = 1)
    {
        heads <- heads + 1
    }
}
DISPLAY(heads)

The code above models flipping a coin 100 times and counts the heads. The random.seed(1) line fixes the random sequence so the run is reproducible: everyone who runs it gets the same tally. Notice the AP pseudocode on the right expresses the same idea with RANDOM(0, 1) and REPEAT 100 TIMES. The two are just different spellings of one simulation.

🎯 What the exam expects

The CED (AAP-3.F) says a simulation is "an abstraction of a real-world phenomenon" that uses "hypotheses" and often randomness. The exam will not ask you to write a full simulation. It asks you to reason about one: why it is used, what randomness and loops are doing, and what its results can and cannot tell you.

✍ Mini Exercise 1 • Predict the output
A simulation tallied 3 wins out of 4 games. What percent does this report?
wins = 3
games = 4
print(wins * 100 // games)

Why We Simulate

If you can flip a real coin, why write a program to flip a fake one? Because for most interesting questions the real experiment is expensive, slow, dangerous, or impossible to repeat. A simulation removes those obstacles:

  • Cheaper and safer: crash-test a bridge design or a rocket engine on a computer before spending money or risking harm.
  • Faster: a program can play out a hundred years of climate or a million dice games in seconds.
  • Repeatable and adjustable: you can run the exact same scenario again, or change one parameter (the rain chance, the number of trials) and immediately see the effect.
  • Observable: you can model things that are hard to watch directly, like the spread of a disease or the motion of galaxies.
import random
random.seed(2)
sixes = 0
trials = 600
for i in range(trials):
    if random.randint(1, 6) == 6:
        sixes = sixes + 1
print(sixes * 100 // trials)
sixes <- 0
trials <- 600
REPEAT trials TIMES
{
    roll <- RANDOM(1, 6)
    IF (roll = 6)
    {
        sixes <- sixes + 1
    }
}
DISPLAY(sixes * 100 / trials)

Here the loop runs 600 trials of rolling a die and counts how often a 6 appears, then reports that count as a percent of the trials. That percent is an estimate of the true probability of rolling a 6. This is the core pattern of a probability simulation: run many trials, tally an outcome, report the fraction.

🎯 More trials, more reliable

The more independent trials you run, the closer the estimate tends to land to the true probability. Six hundred rolls gives a steadier estimate than six rolls. A favorite exam idea: increasing the number of trials improves the reliability of the estimate but never makes it exact.

Randomness, Seeds, and Reproducibility

Randomness is what makes a simulation feel like the real, unpredictable world, and it is also why two runs can give slightly different answers. To make a run reproducible for testing or grading, you set a seed. Seeding fixes the random sequence, so the same seed always produces the same outcomes. That is useful, but the seed has to be set in the right place.

# BUG: seed is inside the loop
for i in range(4):
    random.seed(1)
    print(random.randint(1, 6))
# FIX: seed once, before the loop
random.seed(1)
REPEAT 4 TIMES
{
    DISPLAY(RANDOM(1, 6))
}

On the left the seed is placed inside the loop, so every iteration resets the generator to the same starting point and every roll comes out identical. That is not a simulation of four different rolls, it is the same roll printed four times. The fix on the right seeds once, before the loop, so the four rolls vary as intended while the whole run stays reproducible.

⚠ Runs differ, and that is expected

Because a simulation uses randomness, two runs with different seeds can produce different tallies and different estimates. That variation is not a bug. It is a property of the model. It also means a single run is weak evidence: you judge a simulation by many trials, not one lucky result.

✍ Mini Exercise 2 • Spot the flaw
This model estimates the chance of a 6 using only 5 trials. Why is that estimate unreliable?
import random
random.seed(1)
c = 0
for i in range(5):
    if random.randint(1, 6) == 6:
        c = c + 1
print(c)

Limitations: A Model Is Only as Good as Its Assumptions

A simulation is powerful precisely because it simplifies, and that simplification is also its weakness. Every simulation leaves things out, and its results are only as trustworthy as the assumptions built into it. If the model assumes a fair coin but the real coin is weighted, the simulation will confidently produce wrong conclusions. The output is an approximation of reality that depends entirely on the quality of the model.

The parameterized weather model below assumes a fixed 40 percent chance of rain every single day, independent of the day before. Real weather does not work that way, so this simulation is a rough abstraction. It is still useful for exploring "what if the chance were higher," but its numbers should never be mistaken for a real forecast.

import random
random.seed(4)
rainy = 0
chance = 40
for day in range(30):
    if random.randint(1, 100) <= chance:
        rainy = rainy + 1
print(rainy)
rainy <- 0
chance <- 40
REPEAT 30 TIMES
{
    IF (RANDOM(1, 100) <= chance)
    {
        rainy <- rainy + 1
    }
}
DISPLAY(rainy)

When you evaluate a simulation, ask two questions: are the assumptions reasonable for the question being asked, and were enough trials run to make the estimate stable? A well-built simulation with sound assumptions and many trials gives a useful approximation. A simulation with bad assumptions gives precise-looking numbers that are simply wrong.

✍ Mini Exercise 3 • Fill in the blank
A coin simulation ran 200 flips and tallied 120 heads. Type the whole-number percent it reports.
heads = 120
total = 200
print(heads * 100 // total)
prints:

Key Vocabulary

Term Definition Example
Simulation A program that imitates a real-world process to study it safely and cheaply a flight simulator
Abstraction Keeping the details that matter and removing the rest ignoring air molecules
Assumption A simplifying belief the model is built on, which limits its accuracy "rain chance is 40% daily"
Trial One run of the scenario inside the simulation loop one dice roll
Estimate An approximate result computed from tallying many trials sixes / trials
Seed A fixed starting point that makes a random run reproducible random.seed(1)
📋 Create Performance Task • A simulation as your program

A simulation is a strong choice for the Create Performance Task because it naturally uses a list, a loop, randomness, and a procedure that returns a result, which is exactly the machinery the rubric rewards. The key is to describe it honestly as a model with assumptions, not as reality.

A point-earning example

Imagine a program that estimates the chance of rolling a 6 by simulating many dice rolls and reporting the percentage:

sixes <- 0 trials <- 1000 REPEAT trials TIMES { roll <- RANDOM(1, 6) IF (roll = 6) { sixes <- sixes + 1 } } DISPLAY(sixes * 100 / trials)

In your written response, name the phenomenon, the randomness, and the role of iteration:

  • "My program simulates rolling a fair die, using RANDOM(1, 6) to model one roll."
  • "The loop runs 1000 independent trials and tallies how many are a 6, so the output estimates the probability rather than computing it exactly."
  • "Because the estimate depends on randomness, running more trials makes it more reliable, but it remains an approximation based on the assumption that the die is fair."

The trap to avoid

Do not claim your simulation produces the true, exact answer. A simulation reports an approximation that depends on its assumptions and the number of trials. Saying "this proves the probability is exactly 16 percent" loses the abstraction point. See the full Create Task module →

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Trace
Predict first: compute n times 100, divided by t, before you read the options.
A simulation counted n successes out of t trials and reports the result as a whole-number percent. What is displayed?
n = 45 t = 60 print(n * 100 // t)
Correct. 45 * 100 is 4500, and 4500 // 60 is 75, so it reports 75 percent.
Incorrect. 45 is the raw success count n, not the percent. You still divide by the trials after multiplying by 100.
Incorrect. 60 is the number of trials t, not the percent of successes.
Incorrect. 133 is t * 100 // n, the ratio computed upside down. The successes go on top.
Question 2 of 6Spot the bug
This is meant to run a reproducible simulation of four different dice rolls. What is the bug?
import random for i in range(4): random.seed(1) print(random.randint(1, 6))
Correct. Reseeding to 1 every iteration resets the generator, so randint returns the same value each time and the four rolls are the same, not four different rolls.
Incorrect. range(4) correctly produces four iterations. The problem is the seed placement, not the range.
Incorrect. randint(1, 6) returns any integer from 1 to 6 inclusive, not only the endpoints.
Incorrect. There is no such rule. Calling random repeatedly is exactly how a simulation runs many trials.
Question 3 of 6Spot the bug
This is meant to count how many entries of the list data equal 1. It prints the wrong total. Where is the bug?
c = 0 for k in range(len(data)): c = 0 if data[k] == 1: c = c + 1 print(c)
Correct. c = 0 sits inside the loop, so every iteration wipes the running total. The final value reflects only the last element, not the whole tally.
Incorrect. == is the correct comparison operator. A single = would be an assignment and a syntax error here.
Incorrect. range(len(data)) visits every valid index from 0 to len(data) - 1, so no entry is skipped.
Incorrect. print(c) is outside the loop on purpose, which is correct. It runs once after the loop and does display a value.
Question 4 of 6I, II, III
Which of the following statements about simulations are true?
  • I. A simulation uses simplifying assumptions, so its results are approximations of the real world.
  • II. Running more independent trials generally makes the estimate more reliable.
  • III. A simulation that uses randomness produces the exact same output on every run, no matter the seed.
Incorrect. I is true, but II is also true: more independent trials generally give a more reliable estimate.
Correct. I and II are both true. III is false because different seeds produce different runs. Only a fixed seed makes a run repeat exactly.
Incorrect. III is false. Runs with different seeds differ, so III cannot be part of the answer.
Incorrect. III is false, so not all three are true. Different seeds give different outputs.
Question 5 of 6NOT question
Which of the following is NOT a valid reason to use a simulation instead of a real-world experiment?
Incorrect. Being cheaper and safer IS a valid reason to simulate, so it is not the exception the question asks for.
Incorrect. Repeating and adjusting a scenario IS a real benefit of simulation, so it is not the exception.
Correct. A simulation gives an approximation based on its assumptions. It does not guarantee exactly correct real-world results, so this is not a valid reason.
Incorrect. Modeling hard-to-observe situations IS a genuine benefit, so it is not the exception.
Question 6 of 6Benefit vs limitation
A team increases the number of trials in a probability simulation from 100 to 100,000. Which statement best describes the likely effect?
Incorrect. More trials improve reliability but do not make an estimate exact. A simulation still only approximates.
Correct. More independent trials generally push the estimate closer to the true probability, yet it remains an approximation, never a guarantee.
Incorrect. The number of trials does not change the model's assumptions. Those are built into how each trial works.
Incorrect. Each individual trial is still random. More trials change the reliability of the summary, not the randomness of a single trial.
🎮 Lesson Game
Simulation Station
Trace each simulation and predict the tally or estimate it reports. 8 rounds.
0
Correct
1/8
Round
0
Streak 🔥
🐛 Python • what does it report?
0/8
correct answers
💻 Live Python Code Editor
Practice Problems
Real Python runs right here in your browser. The first time you press Run, the Python engine loads (a few seconds); after that it is instant. Problems build from guided to Create-Task level. Use Hint if you are stuck, and check your output against the target.
Hints used: 0 • Solutions viewed: 0
Level 1 • Guided
Problem 1 of 8 • report a percent
A coin simulation already tallied its results. Report the percent that were heads using integer division.
  • heads is the count of heads
  • total is the number of flips
Target output: 54
Level 2 • Tally a loop
Problem 2 of 8 • count the successes
results is a list where 1 means success and 0 means failure. Loop through it, count the 1s, and print the count. Target output: 4
Level 3 • Seeded simulation
Problem 3 of 8 • count the sixes
The seed is already set so your run is reproducible. Roll a die (random.randint(1, 6)) 10 times, count how many are a 6, and print the count. Target output: 4
Level 4 • Fix the bug
Problem 4 of 8 • move the seed
This should run one reproducible simulation of 12 rolls and count the sixes, but the seed is inside the loop, so every roll is identical. Move the seed so it runs once, before the loop. Target output: 1
Level 5 • Estimate
Problem 5 of 8 • tally a condition
Simulate 60 dice rolls with the seed already set. Count how many rolls are 5 or higher and print that count. Target output: 16
Level 6 • Create Task style
Problem 6 of 8 • parameterized model
A weather model assumes a fixed chance of rain each day. Simulate 30 days: each day, if random.randint(1, 100) is at most chance, it rained. Count and print the rainy days.
  • chance is 40 (a 40 percent daily chance)
Target output: 19
Level 7 • Challenge
Problem 7 of 8 • you write the whole simulation
Open ended. Write the entire simulation yourself. Using the seed provided, simulate rolling a fair die 20 times and print how many of the rolls were a 6. Only the import and seed are given. Target output: 1
Level 8 • Challenge
Problem 8 of 8 • multi-line summary
Open ended. Write the entire simulation yourself. Using the seed provided, flip a coin (random.randint(0, 1), where 1 is heads) 50 times. Print three lines:
  • line 1: the number of heads
  • line 2: the number of tails
  • line 3: the whole-number percent that were heads
Target output: 28 then 22 then 56 on three lines.

Frequently Asked Questions

A simulation is a program that models a real-world phenomenon so you can study it on a computer. It is an abstraction built on assumptions, it often uses randomness to mimic real uncertainty, and it uses iteration to run many trials. Because it simplifies reality, its results are approximations, not exact truths.
Simulations are usually cheaper, safer, and faster than the real thing, and they are easy to repeat and adjust. You can crash-test a design, play out thousands of games, or model something dangerous or slow to observe, all without real-world cost or risk.
Because the simulation uses randomness. Each run draws different random values, so the tallies differ. To make a run reproducible, set a seed; the same seed always produces the same sequence. Different seeds, or no seed, give runs that vary.
No. More independent trials generally make the estimate more reliable and closer to the true value, but the result is always an approximation. A simulation reports an estimate, never a guaranteed exact answer.
A simulation is only as good as its assumptions. It leaves out details, so if the model is wrong (for example, assuming a fair coin that is actually weighted), the results will be confidently wrong. Its output depends on the model and the number of trials, and it approximates rather than proves.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas, $249
Get the Superpack →
🏫
For teachers

The Superpack includes an editable Topic 3.16 slide deck with animated trial-and-tally traces, a benefits-versus-limitations discussion bank, seed-placement bug examples, a print-ready simulation worksheet, and a unit quiz. View what's included →

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]