Problem Solving — CS & DS

Lecture 29 — Simulation as a Way of Knowing

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

Dice Before Data: from model to simulation

cs-113

Two fair dice. Estimate P(sum = 8) by enumeration and by simulation; compare with the exact 5/36.

import random
random.seed(7)
exact = sum(1 for a in range(1, 7) for b in range(1, 7) if a + b == 8) / 36
hits = sum(1 for _ in range(10000) if random.randint(1, 6) + random.randint(1, 6) == 8)
estimate = hits / 10000
print(round(exact, 4), round(estimate, 3))

Expected: exact 0.1389; simulated ~0.138 (seed 7, 10,000 trials)

Cost: O(trials); standard error shrinks as 1/sqrt(trials) - four times the trials buy two times the precision.

Worked example

The Queue Waiting Game: simulate, then sanity-check

cs-114

One server, arrivals every 2 minutes, service takes exactly 1 minute. Simulate 10 customers; what is the maximum wait, and does that match the analytical answer (0)?

free = 0
worst = 0
for i in range(10):
    arrival = 2 * i
    start = max(arrival, free)
    worst = max(worst, start - arrival)
    free = start + 1
print(worst)

Expected: maximum wait: 0 minutes (matches the analytical answer)

Cost: O(customers); the analytical check cost nothing and caught nothing - which is the point of running it.

Worked example

The Exam Gambler: simulation answers what algebra cannot

cs-115

A quiz: answer 4-option questions, 0.25 penalty per wrong answer. Strategy A: answer everything. Strategy B: answer only if you can eliminate one option. Estimate expected scores per question.

import random
random.seed(11)
N = 100000
score_a = score_b = 0
for _ in range(N):
    score_a += 1 if random.random() < 0.25 else -0.25
    score_b += 1 if random.random() < 1 / 3 else -0.25
print(round(score_a / N, 4), round(score_b / N, 4))

Expected: expected score per question: A ~ 0.0625, B ~ 0.167

Cost: O(N) per strategy; precision 1/sqrt(N) as always.

Case attempt · lecture 29
Case cs-113 · Beginner

Dice Before Data

Simulate 10,000 rolls of two dice; compare the sum distribution to theory. Building the generator and trusting the sample.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-113 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-113 — not embedded in this deck.

Case attempt · lecture 29
Case cs-114 · Foundational

The Queue Waiting Game

One server, random arrivals: simulate waits under different arrival rates. See queueing behaviour emerge; locate the collapse point.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-114 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-114 — not embedded in this deck.

Case attempt · lecture 29
Case cs-115 · Intermediate

The Exam Gambler

MCQ exam: guess vs leave blank with negative marking. Simulate strategies over many papers; find the break-even knowledge level.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-115 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-115 — not embedded in this deck.

Case attempt · lecture 29
Case cs-116 · Expert

The Buffon Needle

Estimate pi by dropping needles (or its discrete cousin, random points in a square). Simulation as an estimator; error shrinking like 1/sqrt(n).

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-116 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-116 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. What does the seed control, and what must you report?
  2. When is simulation preferred to analysis?
  3. What shrinks as trials grow?
Expected answers (instructor): reproducibility; report seeds and trial counts with every estimate when the model is realistic but the maths is intractable the estimate's spread — roughly 1/sqrt(n)
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF