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.
No reading — this page is the reading. Runestone doesn’t cover Unit 4 (search, efficiency, limits, simulation), so everything you need is right here.
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.
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.
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.
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
/* 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.
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.
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.
Five questions. Almost every one is fact 1, 2, or 3 from above.
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.