Simulation & Randomness
- Explain what a simulation is and why simulations are useful
- Describe the trade-offs between a simulation and the real experiment
- Use RANDOM to model chance in a program
What a simulation is
A simulation is a program that models a real-world (or imagined) process, letting you experiment with it abstractly instead of in reality. Simulations are built on abstraction: the model includes the features that matter and leaves out the rest. A flight simulator models controls and physics but not every rivet. Because a simulation is a simplified model, its results reflect only the details the designer chose to include — a good simulation captures the essentials, but it is never the full reality.
Why simulate instead of testing for real
Simulations shine when a real experiment would be too expensive, too slow, too dangerous, or impossible. You can crash-test thousands of virtual cars, model a disease spreading through a city, or watch a galaxy form over billions of years — none feasible in a lab. Simulations also let you repeat a scenario many times while changing one variable, isolating its effect. The trade-off is fidelity: a simulation is only as accurate as its model and assumptions, so surprising real-world factors can be missed.
Modeling chance with randomness
Many real processes involve chance, so simulations use randomness. AP pseudocode provides RANDOM(a, b), which returns a random integer from a to b, inclusive, each value equally likely. RANDOM(1, 6) models a die roll; RANDOM(1, 2) a coin flip. Running a random simulation many times and averaging the outcomes estimates real probabilities — roll a virtual die 10,000 times and each face appears about one-sixth of the time. Randomness lets one program produce many different, realistic runs.
A student wants to estimate the chance that rolling two six-sided dice gives a sum of 7, using a simulation. Outline how RANDOM makes this possible.
- 1.Model one die as
RANDOM(1, 6); roll two by calling it twice and adding:sum ← RANDOM(1, 6) + RANDOM(1, 6). - 2.Repeat this many times — say 10,000 — with a loop, counting how often
sumequals 7. - 3.Divide the count of 7s by 10,000 to estimate the probability.
- 4.The estimate approaches the true value (6/36 ≈ 0.167) as the number of trials grows, because more random trials average out chance variation.
RANDOM(1, 6) + RANDOM(1, 6) to simulate a two-dice roll, repeat it thousands of times counting the 7s, and divide by the number of trials. The simulation estimates the real probability (about 0.167) without ever touching physical dice — cheaply and repeatably.An engineering team uses a computer simulation to test how a new bridge design responds to earthquakes. What is the primary advantage of simulating rather than building and shaking a real bridge?
A simulation is only a model: it includes some details and omits others. Its results are as good as its assumptions, so never treat simulation output as a perfect stand-in for reality.
In AP pseudocode, which call would you use to simulate the roll of a single standard six-sided die?
RANDOM(a, b) is inclusive of both endpoints. To model n equally likely outcomes numbered 1 to n, use RANDOM(1, n). Off-by-one endpoints are a favorite exam distractor.
Answer the 2 checkpoints as you read.
Sign in to save your progress