Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท Getting Started

What Is Monte Carlo Simulation?

Learn what Monte Carlo simulation is, why random sampling can answer hard questions, and run your first experiment in Python.

Some questions are too hard to solve with a neat formula. What is the chance that a shuffled deck leaves at least one card in its original place? How likely is a project to finish on time when every task has an uncertain duration? A Monte Carlo simulation answers such questions by playing the situation out many times with random numbers and counting what happens.

The name comes from the Monte Carlo casino in Monaco, a nod to games of chance. The method itself is plain: invent the randomness on a computer, repeat the experiment thousands or millions of times, and read the answer off the results. In this lesson you will see the core idea, the usual recipe, and a first runnable experiment.

The core idea

Suppose you want the probability of an event. Instead of deriving it, you:

  1. Describe one random experiment in code.
  2. Run it N times, each time with fresh random numbers.
  3. Count how many runs produced the event.
  4. Estimate the probability as count / N.

The estimate is not exact, but it gets closer to the truth as N grows. The next lesson series explains how close, and how to measure the error.

flowchart LR
  A["Model one random trial"] --> B["Repeat N times"]
  B --> C["Collect the results"]
  C --> D["Average or count"]
  D --> E["Estimate with an error bar"]

The Monte Carlo loop: model, repeat, collect, summarize.

Why use it?

Three situations make Monte Carlo attractive.

  • No formula exists. Many real systems (queues, weather, financial portfolios) are too tangled for algebra, but easy to simulate.
  • The formula exists but is hard. Integrals in many dimensions are a classic case; you will meet them in the Monte Carlo Integration chapter.
  • You want to check an answer. Even when you can solve a problem by hand, a quick simulation is a strong sanity test.

The price is that results are approximate and random. Two runs give slightly different answers. Good practice means reporting the uncertainty, not only the number.

A first experiment: two dice

Roll two fair six-sided dice. What is the probability that the sum is 7? By counting, there are 6 favorable outcomes out of 36, so the exact answer is 6/36 = 1/6, about 0.1667. Let us see whether simulation agrees.

Python Estimate P(sum of two dice = 7)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the pattern: the estimate wobbles for small N and settles near 1/6 as N grows. The function one_trial is the whole "model"; everything else is bookkeeping.

A question without an easy formula

Take a deck of 10 cards numbered 1 to 10 and shuffle it. A fixed point is a card whose position equals its number. What is the probability of at least one fixed point? The exact answer needs the inclusion-exclusion principle, but the simulation is a few lines.

Python Shuffle and look for fixed points

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You will see the estimate for 10 cards and 52 cards are nearly the same, around 0.63. That is not a coincidence: it approaches 1 - 1/e, a famous result. The simulation hands you the surprise without any heavy theory, and you can then go looking for the reason.

When a probability puzzle feels slippery, simulate it first. The code forces you to state the rules exactly, and the answer tells you what to aim for when you try to prove it.

What Monte Carlo is not

  • It is not a way to prove anything. It gives evidence with a quantified error.
  • It is not the same as "guessing". Each run follows a precise model; only the inputs are random.
  • It does not remove the need for a good model. If the model of the real world is wrong, a million simulated trials will faithfully compute the wrong answer.

Where it is used

Monte Carlo methods appear in physics (particle transport), finance (pricing options and measuring risk), engineering (reliability), computer graphics (path tracing), statistics (Bayesian inference) and machine learning. The later chapters visit several of these. For now, focus on the recipe: model one trial, repeat, summarize.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the basic idea of Monte Carlo simulation?

    Monte Carlo methods estimate quantities by repeating a randomized trial many times and averaging or counting.
  2. 2. You run a simulation with N = 1000 trials and the event happens 170 times. What is the estimated probability?

    The estimate is hits divided by trials: 170 / 1000 = 0.17.
  3. 3. Why do two runs of the same simulation (different seeds) give slightly different answers?

    Each run uses different random numbers, so the estimate varies a little around the true value.
  4. 4. Which statement about Monte Carlo is correct?

    Simulation accuracy depends on N, but a flawed model stays flawed. Good results come with an error estimate.

Summary

  • Monte Carlo simulation estimates quantities by repeating a random experiment many times.
  • The recipe is: model one trial, repeat N times, collect results, average or count.
  • It shines when no formula exists or the formula is hard to evaluate.
  • Estimates are approximate and vary from run to run; report the uncertainty.
  • A simulation is only as good as the model behind it.

Next: Your First Simulation: Estimating Pi