Unit 4 · Algorithms & Simulation · Lesson 4.10

Simulations: Why Fake It

You can’t test a bridge by shaking real bridges. So you build one in software and shake it ten thousand times. Today: what simulations are for, why they use randomness, and what every one of them leaves out.

Big Idea 3 — Algorithms & ProgrammingCED 3.16 · abstraction, fifth visit

Before this lesson

No reading — this page is the reading. Runestone doesn’t cover Unit 4 (search, efficiency, limits, simulation), so everything you need is right here.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Programs, Algorithms region — last content day of the unit. Everything so far asked what algorithms can do. Today you use them to stand in for the real world when the real world is too dangerous, expensive, or slow to test.

2

The Hook

Does this bridge survive an earthquake? You can’t find out by building bridges and shaking them. Too expensive, too dangerous, and you’d need a lot of earthquakes. So engineers build the bridge in software and shake that — thousands of times, with a different earthquake each time.
3

The Idea

A simulation is a program that imitates a real process so you can study it. We use them when the real thing is too dangerous, too expensive, too slow, too big, or impossible to repeat.

Three facts cover most simulation questions on the exam:

1. Simulations use random values to model the variability of the real world.

2. They’re cheaper, safer, and faster than the real thing, and can be re-run with small changes.

3. They are always simplified. Every model leaves details out — and what it leaves out can bias the result.

Fact 3 is abstraction — your fifth visit to it since 1.7. A dice simulation keeps the six faces and the equal chances. It throws away the table felt, gravity, and your wrist. That list of kept and thrown away is the abstraction, and the exam asks about it.

Python

import random
def six_rate(trials):
    sixes = 0
    for i in range(trials):
        if random.randint(1, 6) == 6:
            sixes = sixes + 1
    return sixes / trials

AP Pseudocode

/* RANDOM is built in */
PROCEDURE sixRate(trials)
{ sixes ← 0
  REPEAT trials TIMES
  { IF (RANDOM(1, 6) = 6)
    { sixes ← sixes + 1 } }
  RETURN (sixes / trials) }

Hover or tap a line to light its twin. A list-free version of the Create PT shape: a procedure, a parameter (trials) that changes the outcome, iteration, and selection. Mini-Project 3 adds the list.

4

Try It — The Simulation Sandbox

Part 1. Roll a fair die many times. With few trials, the bars jump around. With many, they settle near 1/6 each (about 16.7%). Same seed = same run, so you can show someone exactly what you got.

trials:
seed:

Part 2. You’re modeling a school fire drill to test whether everyone gets out in under 5 minutes. Tap each feature to keep it in your model or leave it out. Then check your abstraction.

5

Vocabulary

simulation Programs
A program that imitates a real-world process so it can be studied — usually using random values to model variability.
model Programs
The simplified version of reality a simulation runs on: what it represents, and how.
abstraction Bits → Programs
Keeping the details that matter for the question and removing the ones that don’t. In a simulation, the list of what’s kept and what’s left out.
6

Check

Five questions. Almost every one is fact 1, 2, or 3 from above.

0 of 5 answered
7

Impact Check

Models make real decisions

Simulations decide where hospitals send ventilators, how tall a levee should be, and how many buses a city runs. They’re often the best tool available — and they’re only as good as what they include.

A flood model that leaves out old storm drains in one neighborhood will say that neighborhood is safe. The simulation isn’t lying; it’s answering a simpler question than the one people think they asked. Asking “what did this model leave out?” is a skill citizens need, not just programmers.

8

Connections

Came from 3.17–3.18 — RANDOM and the dice lab. Now the randomness has a purpose.
Came from 1.7 — abstraction again: this time it’s the thing that makes a model both useful and risky.
Next in 4.11–4.12 — Mini-Project 3: build your own simulation and write about what it leaves out.
Returns in Unit 5 — many trials make a dataset. Simulation results become data.
← 4.9 Undecidable Problems