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:
- A deterministic formula doesn't exist or is too complex to evaluate.
- The problem lives in many dimensions (where grid-based methods are impractical).
- An approximate answer is acceptable, and you can afford many random samples.
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
Sample: Pick a random point $(x, y)$ with $x, y \in [-1, 1]$.
-
2
Test: Compute $d = x^2 + y^2$. The point is inside the circle if $d \le 1$.
-
3
Count: Track hits $h$ and total throws $N$.
-
4
Estimate: $\pi \approx 4 \cdot h / N$. Repeat forever — the estimate converges.
π Convergence: How Sample Count Affects Accuracy
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.
Monte Carlo Integration
The dart-throwing idea generalises. To integrate any function $f(x)$ over $[a, b]$:
- Draw $N$ random samples $x_i$ uniformly in $[a, b]$.
- Compute the mean: $\bar{f} = \frac{1}{N} \sum f(x_i)$.
- Estimate: $\int_a^b f(x)\, dx \approx (b - a) \cdot \bar{f}$.
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:
- Stratified sampling: divide the domain into equal sub-regions and sample from each — reduces clustering.
- Importance sampling: throw more darts where the function is largest, then correct the weights. Crucial in path tracing (3D rendering).
- Control variates: use an analytically known related function to reduce variance around the estimate.
- Quasi-Monte Carlo: replace pseudo-random numbers with low-discrepancy sequences (Halton, Sobol) for better coverage and $O((\log N)^d / N)$ error.
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.