Monte Carlo Methods — Using Randomness to Solve Real Problems

When the exact answer is too expensive to compute, you can throw darts at it. Monte Carlo methods turn random sampling into surprisingly accurate solutions — and they power several simulations on this site.

What is a Monte Carlo Method?

The name comes from the casino in Monaco — the method is named after the city famous for gambling randomness, not the city itself. Monte Carlo methods are any algorithms that use random sampling to get numerical results. They are especially useful when:

The accuracy of a Monte Carlo estimate improves as $\frac{1}{\sqrt{N}}$ — doubling accuracy requires four times as many samples. That sounds slow, but in high-dimensional spaces it beats every competing method.

Estimating π by Throwing Darts

The most famous Monte Carlo demonstration involves a unit circle inside a 2×2 square. If you throw darts uniformly at the square, the fraction that land inside the circle equals $\pi/4$ — because the circle area is $\pi r^2 = \pi$ and the square area is $4$.

  1. 1
    Sample: Pick a random point $(x, y)$ with $x, y \in [-1, 1]$.
  2. 2
    Test: Compute $d = x^2 + y^2$. The point is inside the circle if $d \le 1$.
  3. 3
    Count: Track hits $h$ and total throws $N$.
  4. 4
    Estimate: $\pi \approx 4 \cdot h / N$. Repeat forever — the estimate converges.

π Convergence: How Sample Count Affects Accuracy

N = 100
≈ 3.16
N = 1 K
≈ 3.14
N = 10 K
≈ 3.141
N = 1 M
≈ 3.1416

Note the bar lengths do not reach 100% — the estimate converges but never reaches the exact value. The error $\sigma \propto 1/\sqrt{N}$ means 100× more samples buys only one extra decimal place.

function estimatePi(N) { let hits = 0; for (let i = 0; i < N; i++) { const x = Math.random() * 2 - 1; // [-1, 1] const y = Math.random() * 2 - 1; if (x*x + y*y <= 1) hits++; // inside unit circle? } return 4 * hits / N; // π ≈ 4 · (area_circle / area_square) }

Monte Carlo Integration

The dart-throwing idea generalises. To integrate any function $f(x)$ over $[a, b]$:

This works because the mean of $f$ times the interval width equals the integral. More importantly, for a $d$-dimensional integral, the error is still $O(1/\sqrt{N})$ regardless of $d$ — while grid-based quadrature costs $O(N^{1/d})$ per dimension. In 20 dimensions, Monte Carlo wins by an enormous margin.

Where We Use Monte Carlo on This Site

Monte Carlo π Simulation

The Monte Carlo π simulation runs the dart-throw algorithm live, plotting the estimate convergence in real time. You can watch the standard deviation bar shrink as $N$ grows.

Disease Spread (Stochastic SIR)

The Disease Spread simulation uses random contact events rather than differential equations. Each timestep, each infected agent contacts $k$ random others — the macro-level S/I/R curves emerge from thousands of individual Monte Carlo draws.

Bitcoin Mining Nonce Search

The Bitcoin Mining simulation is essentially a Monte Carlo hash search: try random nonces until one satisfies the difficulty target. It demonstrates why mining is probabilistic.

Diffusion-Limited Aggregation

Diffusion and crystal growth use random walks (a Monte Carlo process) to determine particle trajectories, which collectively produce fractal, branching crystal structures.

Variance Reduction: Getting More for Less

Basic Monte Carlo throws darts uniformly. But smarter strategies can achieve the same accuracy with fewer samples:

Monte Carlo methods power the global illumination in every modern 3D film and game. Path tracing — the core algorithm behind Pixar and NVIDIA RTX — is Monte Carlo integration of the rendering equation over hemisphere directions. Each "bounce" is a random sample.

When Not to Use Monte Carlo

Monte Carlo is overkill for low-dimensional smooth integrals where Gaussian quadrature or Simpson's rule converges faster. It is also a poor choice when you need a deterministic, reproducible result every time. And it cannot tell you why the answer converges to what it does — for insight, analytical methods will always win.

As a rule of thumb: if dimensions $d > 4$ and accuracy to 2-3 significant figures is enough, Monte Carlo is the right tool.