Monte Carlo: a tour in six stops

Monte Carlo answers a question by generating many random versions of the situation and counting what happens. These six small experiments each add one thing the method can do, from two dice to an agent-based model. Drag the sliders, press the buttons, and read the “Try this” box under each one. They accompany a seminar of Social Science Simulations at the University of Tartu; the slides and a Python notebook with the same experiments are linked at the bottom.

The recipe, once

Monte Carlo answers a question by generating many random versions of the situation and counting what happens. Every example below, from dice to a city, is built from the same three parts: a model of the situation with something uncertain in it; a random draw that produces one concrete version of that uncertain thing; and repetition and counting, thousands of draws, then a share, an average or a histogram. The average of many draws settles on the true value (the law of large numbers), and the scatter of the estimate shrinks like 1/√N: a hundred times more draws, ten times less scatter.

Each stop ends with a box, Where is the Monte Carlo here?, that names those three parts for that experiment and says what the stop adds to the story. The recipe never changes; only what sits inside the model does, and it grows at every stop.

1

Two dice, and the first Monte Carlo

Roll two dice and add them. How often is the sum 7? The exact answer is 6/36 ≈ 16.7%. The simulation does not know that: it rolls, adds, and counts. Watch how the picture sharpens as you roll more. The histogram and the estimate come from the same rolls: P(7) is simply the height of the middle bar.

—
simulated P(sum = 7)
16.7%
exact
—
error (percentage points)

Share of each sum: bars = simulated, dots = exact

Try this

ParameterWhat it meansWhat to watch
RollsHow many times the two dice are thrownBars approach the dots as the number grows. The probability 1/6 itself never changes; what shrinks, roughly like 1/√N, is the scatter of the estimate around it
Roll againA fresh set of random rolls at the same NAt small N each press gives a visibly different picture: the estimate is itself random

Find the smallest number of rolls at which the estimate stays within one percentage point of 16.7% press after press.

Where is the Monte Carlo here?

The model
Two dice, added together. Everything else about the world is ignored.
The random draw
One roll: two random whole numbers from 1 to 6.
Repeat and count
Roll N times; count how often the sum is 7; divide by N. That share is the estimate of the probability.
What this stop adds
The recipe in its simplest form, with a known answer (1/6) to check it against. From here on the recipe never changes; only what sits inside the model does.
What this stop shows
  • A probability can be measured by counting, without any formula.
  • The estimate is random; more runs make it better, with diminishing returns.
2

Darts, π, and measuring with randomness

Throw darts at random onto a square with a quarter circle inside. The share that lands inside the circle (inside ÷ thrown) estimates its share of the area, π/4; the circle-area formula then turns that area into π: π ≈ 4 × inside ÷ thrown. Nobody is gambling here: randomness is being used as a ruler.

—
estimate of π
—
± two standard errors, from the darts alone
—
actual error (only known because we know π = 3.1416)

The board (first 4,000 darts shown)

Running estimate as darts accumulate (dashed = true π)

Try this

ParameterWhat it meansWhat to watch
DartsHow many random points are thrown at the squareThe running estimate wanders, then settles near 3.14 but never becomes exact: always quote a margin
Throw againA fresh set of darts at the same numberTwo runs give two different paths and two different final estimates
± two standard errorsThe margin computed from the data: 4 × √(p(1−p)/N) × 2, with p the share insideIt needs no knowledge of π, and it shrinks like 1/√N. The actual error is usually inside it. This is the number to report

How many darts are needed before the estimate is reliably within 0.01 of π? Does the 1/√N rule predict it? The same trick measures the area under any curve; the notebook lets you change the curve.

Where is the Monte Carlo here?

The model
A square board with a quarter circle drawn inside it. No chance is involved in the question itself.
The random draw
One dart: a random point in the square, two random numbers between 0 and 1.
Repeat and count
Throw N darts; count the share inside the circle. That share estimates an area; multiplying by 4 turns the area into π.
What this stop adds
The same recipe, but what we count now measures a number rather than a chance, and we learn to attach a margin to it from the data alone (the standard error).
What this stop shows
  • Monte Carlo can estimate numbers, not just probabilities, by turning a measurement into a count.
  • It does not care how many dimensions the problem has, which is its great advantage.
3

A referendum, and taming the future

Polls say 52% will vote yes, but polls miss by about three points and turnout is uncertain too. A single number cannot say whether it passes. Simulate thousands of election nights, each with its own random poll error and turnout effect, and read off a probability and a range.

—
probability yes wins
—
median result
—
90% of outcomes within

Final yes share in each simulated election: green = passes, orange = fails

Try this

ParameterWhat it meansWhat to watch
Polled yes %What the last polls sayMove it toward 50: the pass probability falls fast near the line
Poll error ±How wrong polls usually are (standard deviation, in points)Widen it: the histogram flattens and the probability moves toward 50/50
Turnout tiltAverage points gained or lost through turnoutShifts the whole histogram left or right
SimulationsHow many election nights are playedAt 100 the histogram is ragged; at 10,000 it is smooth

Find the poll lead at which the pass probability reaches 90%. Then ask: which input would you most want to measure better, the polls or the turnout?

Where is the Monte Carlo here?

The model
An election night: the final yes-share equals the polled share plus a poll error plus a turnout effect. Three inputs, two of them uncertain.
The random draw
One election: a random poll error from a bell curve with SD 3, and a random turnout effect from a bell curve with mean −1 and SD 1.
Repeat and count
Play N election nights; keep every final result. Count the share above 50% for the probability of passing; sort them for the median and the 5–95% range; draw them all as the histogram.
What this stop adds
The model now has several uncertain inputs at once, and what we keep is the whole distribution of outcomes, not one number. This is the form Monte Carlo takes in risk analysis, project planning and forecasting.
What this stop shows
  • Replace every fixed input by a distribution; the output becomes a distribution too.
  • Report a probability, a median and a range. The histogram is more honest than any single number.
4

Planning a survey by simulating fake data

You will compare two groups and expect a modest difference. How many people do you need to detect it? Invent a world where the difference is real, simulate hundreds of fake studies, and count how many of them detect it. That share is the statistical power: the share of whole studies, across many imagined repeats, that detect the effect. The usual target is 80%. The one input you must assume is the effect size d: the difference between the group means divided by the standard deviation of the measurement (0.2 small, 0.5 medium, 0.8 large). The simulation cannot tell you what effect matters; you decide that first.

—
power (share of fake studies that detect the effect)
—
verdict
—
batches of 400 run at this setting

Power against sample size for this effect size. To read it: follow the line to the dashed 80% target and read the sample size below. Dot = your setting

Try this

ParameterWhat it meansWhat to watch
People per groupSample size of each of the two groupsPower rises with n but flattens: going from 100 to 200 buys little for a medium effect
Effect size dHow big the real difference is, in standard-deviation units (0.2 small, 0.5 medium, 0.8 large)Small effects need very large samples; at d = 0.2 the verdict stays red until several hundred per group. Set d = 0: power settles near 5%. With no real effect, one study in twenty still “detects” something: that is the false-alarm rate α
Run 400 fresh studiesRepeats the whole batch with new random dataThe power moves a little each press: it is itself a Monte Carlo estimate with its own scatter

For a medium effect, find the smallest group size that reaches 80%. Then press the button several times at that setting and watch the number jitter. The notebook extends this to unequal groups and to checking whether a confidence interval really covers 95%, including two ways to break that check on purpose.

Where is the Monte Carlo here?

The model
A whole study: two groups of n people, a questionnaire score for each, and the statistical test you would run on the real data. The difference between the groups is set by you, so the truth is known.
The random draw
One fake study: n random scores for group A, n random scores for group B shifted by the effect size, then the test, which answers yes or no.
Repeat and count
Run 400 fake studies; count the share that answered yes. That share is the power. At effect size 0 the same count gives the false-alarm rate.
What this stop adds
The model is now a study, data and analysis included, simulated in a world where we wrote the truth ourselves, so every miss is a known mistake. This is how study designs are tested before any data are collected.
What this stop shows
  • You can test a study design by simulation before collecting any data.
  • The same trick checks whether any statistical method behaves as advertised.
5

Schelling's city: Monte Carlo over an agent-based model

A grid of houses, two kinds of household, one rule: each household has a threshold, the minimum share of its eight neighbours it wants to be its own kind (0.30 means “at least 3 of my 8 like me”), and moves to a random empty house if its street falls below it. The slider calls this “tolerance”, but note that a higher value means a more demanding household. Segregation is measured as the average household's share of similar neighbours: about 0.5 for a mixed city, near 1.0 when fully separated. Schelling (1971) showed that mild thresholds produce strong collective segregation: households stop at 0.30, the city ends near 0.75. One run is an anecdote; the sweep runs the model many times and reveals where the jump is.

—
segregation (average share of similar neighbours)
—
unhappy households
0
rounds

One run of the city (40 × 40)

The sweep: segregation against tolerance, averaged over many runs

Try this

ParameterWhat it meansWhat to watch
ToleranceMinimum share of similar neighbours a household wantsClusters appear already at 30%; above about 65% nobody can be satisfied, the city churns and never settles
SeedOne seed = one complete random city: the starting arrangement and the order of moves. Like one roll, one dart or one fake study at the earlier stopsSame threshold, different seed: clusters land elsewhere and the segregation number differs. Which run is “the result”? Neither
Runs per pointHow many seeds are averaged at each threshold in the sweepMore runs: smoother curve, narrower band. In the curve, dots = the estimate, band = the spread, crosses = a single-run anecdote. The loop over seeds is the dice loop of Stop 1; the loop over thresholds is the only new thing
Empty shareFraction of empty housesMore empties make moving easier: does the jump in the curve move?

Run one city at 30% with three different seeds and note the segregation each time. Then press Sweep and find where the curve jumps. Rule for every agent-based model: never report a single run.

Where agent-based models came from, and where they are going: Ulam, von Neumann and Metropolis, Conway’s Game of Life, Schelling’s 1971 city, Axelrod’s tournaments, and the Santa Fe Institute’s artificial stock market and Sugarscape, are told with pictures in the Santa Fe Institute’s illustrated history Agent-based modeling: worlds within worlds.

Where is the Monte Carlo here?

The model
An agent-based model: a city of households, each with a threshold, moving house by a simple rule. Each run has its own random start and its own random moves.
The random draw
One seed: one complete random city, start and moves together, run until it settles. A single run is one draw, exactly as one roll was one draw at Stop 1.
Repeat and count
At each threshold run the city many times with different seeds; average the segregation for the estimate, take the 5–95% range for the band. The loop over seeds is the dice loop; the loop over thresholds is the only new thing. That is the sweep.
What this stop adds
The thing inside the recipe is now a whole simulation. Wrapping the dice loop around it turns single-run anecdotes into a curve with a visible threshold and a visible margin. Every agent-based model should be reported this way.
What this stop shows
  • A single run of a random model is one draw from a distribution of outcomes.
  • Sweeping a parameter over many runs turns a simulation from a story into evidence, and reveals thresholds no single run can show.
6

Why the spread matters: Granovetter's crowd

Where did Schelling's 30% come from? Nowhere: it stands in for an attitude that real people would report differently, so there is really a distribution of thresholds, not one number. Granovetter (1978) showed how much that matters with a model of a crowd. Each person has a threshold: the share of the others who must already be protesting before this person joins. Some need nobody (they start things), most need a few, some need almost everyone. Each round, everyone whose threshold has been reached joins, and the next round sees a larger crowd. Two crowds with the same average threshold can end in a full riot or in nothing at all, depending only on how much people differ. The left panel shows the crowd's thresholds; the right panel shows what happens.

Granovetter's own example makes the mechanism plain. Imagine 100 people whose thresholds are 0, 1, 2, 3, … up to 99 (each person needs one more than the last). The person with threshold 0 starts; that is one person, which is enough for the person with threshold 1; now two, enough for threshold 2; and so on until all 100 are in. Now change one person: replace the threshold 1 by 2. The starter still starts, but nobody needs exactly one other person any more, so the chain breaks at the first link and the crowd stays at one. Same crowd to within a single person, same average to within a hundredth, and a riot has become nothing. A cascade needs an unbroken ladder of thresholds; spread is what supplies the rungs. That is why a distribution of attitudes, not an average, is what the model needs.

—
final share who join
—
people who need nobody (threshold 0): the spark
—
rounds until it settles

The crowd: how many people have each threshold (1,000 people)

The cascade: share who have joined, round by round (grey = the comparison run)

Try this

ParameterWhat it meansWhat to watch
Mean thresholdOn average, the share of others a person needs to see protesting before joiningLower means easier cascades, but the mean alone does not decide the outcome
Spread (SD)How much people differ in their thresholds: the width of the histogramKeep the mean at 0.25 and move the spread slowly from 0.05 upward. At 0.10 almost nobody joins; between about 0.12 and 0.15 the final turnout flips from 0% to 100%, with the average attitude unchanged
CompareDraws the cascade for spread 0.10 and 0.15 together at your meanSame mean, opposite outcomes. The spark needs a chain: each round must bring enough people whose threshold has just been reached
SeedWhich random crowd is drawn from the distributionNear the flipping point the seed alone can decide the outcome: another reason never to report a single run

A single number for an attitude cannot capture this; a distribution can. Turning attitudes into numbers (pseudo-quantification) and carrying the uncertainty of that translation into a model is the subject of the next seminar. Three further uses of Monte Carlo, bootstrapping, sensitivity analysis and Markov chain Monte Carlo, are worked through in the notebook.

Where is the Monte Carlo here?

The model
A crowd of 1,000 people, each with a threshold: the share of others who must already be protesting before this person joins. The thresholds come from a distribution with a mean and a spread.
The random draw
One seed: one crowd, 1,000 thresholds drawn from the bell curve you set. Then the cascade runs with no further randomness: round by round, everyone whose threshold has been reached joins.
Repeat and count
Here the randomness is in who the crowd is, not in what they do. Change the seed near the flipping point and the outcome can flip; to report this model honestly you would run many crowds and give the share that riot, as in Stop 5.
What this stop adds
The spread of the thresholds, not their average, decides the outcome. A single number for an attitude cannot carry that; a distribution can, which is what the next seminar is about.
What this stop shows
  • The shape of the uncertainty, not just its average, can decide what a model predicts.
  • Monte Carlo quantifies the uncertainty inside a model; it cannot tell you whether the model is right.

Materials

These experiments accompany a seminar, “Monte Carlo Analysis and Python-based Simulations”, of the course Social Science Simulations (SVUH.00.300) at the University of Tartu, autumn 2026. The slides and a Python notebook with every experiment, its code, sliders and exercises are free to use. The story continues in the From words to numbers tour, which carries the uncertainty of a translated attitude into a model.

Text and figures: CC BY 4.0. Code: MIT. References: Metropolis & Ulam (1949); Metropolis (1987); Schelling (1971); Granovetter (1978); Cohen (1988); Efron (1979).