Simulation Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Simulation.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Using a computer program to model and experiment with a real-world system or process. Simulations represent key variables and their relationships mathematically, allowing you to test scenarios, make predictions, and explore outcomes without real-world cost or risk.

A virtual experimentβ€”test ideas without real-world consequences.

Read the full concept explanation β†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Simulations simplify reality by choosing which variables to model and which details to ignore.

Common stuck point: Simulations are only as good as their underlying assumptionsβ€”garbage in, garbage out.

Sense of Study hint: When building a simulation, first identify the key variables and relationships that matter for your question. Build the simplest model that captures those relationships, run it with known inputs to validate it, then use it to explore new scenarios. Always state your assumptions clearly.

Worked Examples

Example 1

easy
A weather app uses a computer simulation to predict tomorrow's weather. What is a simulation and why is it useful?

Answer

A simulation models a real-world process using computation. It is useful for making predictions and testing scenarios without real-world cost or risk.

First step

1
Step 1: A simulation is a computer model that imitates a real-world process or system using mathematical rules and data.

Full solution

  1. 2
    Step 2: The weather simulation uses current data (temperature, pressure, wind) and applies physics equations to predict future conditions.
  2. 3
    Step 3: Simulations are useful because they let us explore scenarios that would be too expensive, dangerous, or time-consuming to test in real life.
Simulations are used in weather forecasting, flight training, medical research, and engineering. They rely on abstraction β€” modelling the essential features while ignoring unnecessary detail.

Example 2

medium
A traffic simulation models cars on a road. Each car has a speed and position. Every second, each car moves forward by its speed, unless the car ahead is too close (then it slows down). What simplifications (abstractions) does this simulation make?

Example 3

easy
A dice-roll simulation outputs 1,3,5,2,6 across 5 trials. Estimated mean roll?

Example 4

medium
A weather model predicts 80% chance of rain. Out of 100 such forecasts, 60 turn out rainy. Is the model well-calibrated?

Example 5

hard
Two integration step sizes: h=0.1h=0.1 gives error ∼0.01\sim 0.01; what error do you expect at h=0.05h=0.05 for a first-order method?

Example 6

challenge
A climate model is run with two different seeds, yielding diverging trajectories after 50 years. How do scientists extract a useful prediction?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

medium
You are designing a simulation to model the spread of a disease through a school. What variables would you include, and what would you simplify?

Example 2

hard
A dice-rolling simulation generates random numbers 1-6 to simulate rolling a die 1000 times. Why might the simulated results differ from the theoretical probability (1/6 each)? What would make them converge?

Example 3

easy
A simulation uses St+1=St+1S_{t+1} = S_t + 1 with S0=0S_0 = 0. What is S3S_3?

Example 4

easy
Does a simulation model every detail of reality, or a chosen subset of variables?

Example 5

easy
Why run a simulation instead of the real experiment?

Example 6

easy
A dice simulation should produce values in what range for a standard die?

Example 7

easy
If a simulation's assumptions are wrong, are its predictions reliable?

Example 8

easy
A growth model is St+1=2StS_{t+1} = 2 S_t with S0=1S_0 = 1. What is S2S_2?

Example 9

easy
Adding more and more variables to a simulation tends to make it ____.

Example 10

easy
A simulation steps once per timer event. What programming style does it rely on?

Example 11

medium
With St+1=St+2S_{t+1} = S_t + 2 and S0=5S_0 = 5, find S4S_4.

Example 12

medium
A coin-flip simulation runs 1000 trials and gets 600 heads. Estimated probability of heads?

Example 13

medium
A model rounds St+1=1.1StS_{t+1} = 1.1 S_t to 1 decimal each step, S0=1.0S_0 = 1.0. Compute S2S_2.

Example 14

medium
A population model St+1=St+10S_{t+1} = S_t + 10 starts at 50. After how many steps does it first exceed 100?

Example 15

medium
Two simulations start identically but one uses step size 1.0 and the other 0.5 (half steps, twice as many). Which generally tracks reality more closely?

Example 16

medium
A simulation predicts a bridge holds 10 tons, but the real bridge fails at 6. What should you do before trusting future predictions?

Example 17

medium
A queue simulation processes one customer per tick and a new customer arrives every other tick. Starting empty, how many wait in queue after 4 ticks?

Example 18

medium
A simulation St+1=Stβˆ’3S_{t+1} = S_t - 3 models a draining tank starting at S0=20S_0 = 20. What is S5S_5?

Example 19

medium
A traffic-light simulation cycles green->yellow->red every tick. Starting on green, what color is it after 4 ticks?

Example 20

challenge
In St+1=St(1+r)S_{t+1} = S_t(1 + r) with r=0.05r = 0.05 and S0=100S_0 = 100, give a closed form for StS_t and compute S3S_3 to 2 decimals.

Example 21

challenge
A simulation has 0.1% error per step that compounds multiplicatively. After 1000 steps, roughly what factor of error accumulates? Use (1.001)1000β‰ˆe(1.001)^{1000}\approx e.

Example 22

challenge
Design choice: model a forest fire either cell-by-cell on a grid or with a single aggregate 'percent burned' variable. State one advantage of each and which suits predicting fire shape.

Example 23

easy
A simulation uses St+1=St+5S_{t+1} = S_t + 5 with S0=2S_0 = 2. Find S4S_4.

Example 24

easy
A simulation of a flipped coin generates a number 0 or 1. To estimate P(heads)P(\text{heads}), what should you do with the outputs?

Example 25

easy
With St+1=Stβˆ’1S_{t+1} = S_t - 1 and S0=10S_0 = 10, at what step does StS_t first equal 0?

Example 26

easy
A growth model St+1=3StS_{t+1} = 3 S_t has S0=1S_0 = 1. Compute S3S_3.

Example 27

easy
Which is an abstraction a traffic simulation might make: (a) cars are points, (b) every license plate is modeled?

Example 28

medium
With St+1=St+tS_{t+1} = S_t + t and S0=0S_0=0, find S4S_4.

Example 29

medium
A coin simulation gives 480 heads in 1000 flips. Estimated P(heads)P(\text{heads})?

Example 30

medium
A predator-prey model needs to track populations and a contact rate. Name one variable to keep and one detail safe to drop.

Example 31

medium
A simulation St+1=Stβ‹…0.9S_{t+1} = S_t \cdot 0.9 with S0=100S_0 = 100 models radioactive decay. Compute S2S_2.

Example 32

medium
A simulation's predictions diverge from reality after step 100. What is most likely the cause: random noise or compounding model error?

Example 33

medium
A simulation runs 10000 trials per minute. How many trials in 6 minutes?

Example 34

medium
Simulation St+1=St+1S_{t+1} = S_t + 1 if St<5S_t < 5, else St+1=Stβˆ’1S_{t+1} = S_t - 1. Starting at S0=4S_0 = 4, find S3S_3.

Example 35

medium
An SIR disease model tracks SS, II, RR counts. Which simplification trades realism for tractability: (a) homogeneous mixing, (b) tracking each individual home address?

Example 36

medium
Compounding model: St+1=St(1+0.1)S_{t+1} = S_t(1 + 0.1) with S0=100S_0=100. Compute S2S_2.

Example 37

medium
A queue simulation: arrivals occur every 2 ticks, service takes 3 ticks. After 12 ticks starting empty, how many customers entered service?

Example 38

hard
Recurrence St+1=2Stβˆ’1S_{t+1} = 2S_t - 1, S0=2S_0=2. Find S4S_4.

Example 39

hard
A Monte Carlo estimator uses NN samples and has error ∝1/N\propto 1/\sqrt{N}. To halve error, how many more samples are needed?

Example 40

hard
A flight simulator passes all known test scenarios but a real plane crashes when a flap fails. What was the model missing?

Example 41

challenge
A logistic model St+1=St+0.1St(1βˆ’St/100)S_{t+1} = S_t + 0.1 S_t (1 - S_t/100), S0=10S_0=10. Compute S1S_1 to 2 decimals.

Background Knowledge

These ideas may be useful before you work through the harder examples.

algorithmabstraction