Skip to main content
Back to module
Foundations for Math, Statistics & Analytics Careers · Thinking in Numbers

Pairing math with computing

Reading31mLesson 2 of 3 in this module

What you'll learn

  • Run a quick Monte Carlo simulation in Python
  • Compare a simulated probability with an exact answer
  • Explain how the number of trials and a random seed affect precision and repeatability

Run a quick Monte Carlo simulation in Python and compare it with an exact probability.

Why this matters

Math on paper is powerful but slow, and real problems are messy. Employers in statistics, actuarial work and operations research want people who can turn math into working code: load real data, run a model, simulate thousands of scenarios and check the answer. Math plus computing plus knowledge of a real domain (insurance, health, logistics, finance) is the combination that gets people hired.

What computing adds

  • Scale. A spreadsheet struggles with millions of insurance policies or delivery records; code handles them routinely.
  • Simulation. When a problem is too complicated to solve exactly, you can simulate it many times and measure what happens.
  • Repeatability. A script can be rerun next month on new data, checked by a colleague and audited.
  • Checking. Code lets you test a formula against a brute-force calculation, which catches mistakes.

The common tools are Python (with libraries such as pandas, NumPy and SciPy) and R for statistics; SQL for getting data out of databases; spreadsheets for quick work and communication; and specialised software for optimisation solvers and actuarial models. You do not need all of them. Python plus SQL plus a spreadsheet covers a lot of entry-level work.

Monte Carlo simulation

A Monte Carlo simulation estimates a probability or an average by running a random experiment many times and counting results. The name comes from the Monte Carlo casino, and the method became widely used in the 1940s during early computer work at Los Alamos.

The recipe:

  1. Write code that runs one random trial of the situation.
  2. Repeat it many times (thousands or millions).
  3. Count how often the outcome you care about happens, and divide by the number of trials.
  4. Check that the answer settles down as trials increase.

The error of a simulated probability shrinks roughly in proportion to one over the square root of the number of trials. So 100 times more trials gives about 10 times more precision.

Worked example: at least one six in four rolls

What is the chance of getting at least one six when you roll a fair die four times? This is a famous puzzle from the 1600s, discussed by Pascal and Fermat.

Exact answer. It is easier to find the chance of no sixes. Each roll misses a six with probability 5/6, and the rolls are independent, so the chance of no sixes in four rolls is (5/6)⁴ ≈ 0.4823. The chance of at least one six is 1 − 0.4823 = 0.5177, slightly better than even.

Simulated answer. In Python:

Python
import random

rng = random.Random(42)
trials = 20000
hits = 0
for _ in range(trials):
    rolls = [rng.randint(1, 6) for _ in range(4)]
    if 6 in rolls:
        hits += 1
print(hits / trials)   # about 0.52

With 100 trials the estimate might be anywhere from about 0.42 to 0.62. With 20,000 trials it is usually within about 0.01 of 0.5177. The exact formula and the simulation agree, and each checks the other.

Why simulate when the exact answer is available? Because real problems often have no tidy formula: an insurer's total claims next year, with thousands of policies and different claim sizes; a queue with breaks, shift changes and rush hours; a portfolio with correlated assets. Analysts use the same four-step recipe on those problems, and they test their code first on a case like this one where the exact answer is known.

Seeds and repeatability

Computers use pseudo-random number generators. Setting a seed (here 42) makes the "random" sequence the same every run, so a colleague can reproduce your exact numbers. Use a seed for reports and testing; change it to check that your conclusion does not depend on one lucky sequence.

Common mistakes

  • Too few trials. A few hundred trials can be badly off. Increase the count until the answer settles.
  • Not checking against a known case. Test your simulation where an exact answer exists before trusting it elsewhere.
  • Hidden dependence. The exact formula assumed independent rolls. If your simulation or real system breaks that assumption, the answers differ.
  • No seed. Results that change every run are hard to review and debug.
  • Learning tools without math. Code runs a wrong model just as happily as a right one.

Quick recap

  • Employers value math combined with programming and domain knowledge.
  • Monte Carlo: simulate one trial, repeat many times, count, check convergence.
  • More trials means more precision, roughly by the square root; seeds make results repeatable.

Try it

Open the Do tab and finish the simulation by writing one line of Python, then watch the estimate approach 0.5177.

Key terms (5)

Monte Carlo simulation
A method that estimates a probability or average by running a random experiment many times and counting the results.
Repeatability
The ability to rerun the same analysis, for example next month or by a colleague, and get results that can be checked and audited.
independent
Describes events where one outcome does not affect another, such as separate rolls of a fair die.
pseudo-random
Numbers produced by a deterministic generator that look random but follow a sequence fixed by a starting value.
seed
The starting value of a pseudo-random generator; setting it makes the random sequence the same on every run.

Recap

  • Employers value math combined with programming and knowledge of a real domain.
  • Monte Carlo: simulate one trial, repeat many times, count the outcome, and check that the estimate settles.
  • Error shrinks roughly with one over the square root of the trials, so 100 times more trials gives about 10 times more precision.
  • Test a simulation on a case with a known exact answer, such as 1 - (5/6)^4 = 0.5177.
  • A fixed seed makes results repeatable for reports and reviews.
Also in this lesson: