Logo

Computing π with Monte Carlo

The Monte Carlo method is a statistical technique that uses random sampling to obtain numerical results. One of its elegant applications is approximating the value of π (pi).

How it works

Imagine a square with a side length of 2, centered at the origin. Inside this square, we inscribe a circle with radius 1. The area of the square is 4, and the area of the circle is π.

If we randomly place points inside the square, the probability of a point falling inside the circle is:

P(inside circle)=Area of circleArea of square=πr2(2r)2=π4P(\text{inside circle}) = \frac{\text{Area of circle}}{\text{Area of square}} = \frac{\pi r^2}{(2r)^2} = \frac{\pi}{4}

Therefore, if we generate N random points and count how many fall inside the circle (let's call this count M), we can approximate π as:

π4MN\pi ≈ 4* \frac{\text{M}}{\text{N}}

The more points we generate, the more accurate our approximation becomes.

Mathematical Insight

The Monte Carlo method works because we're essentially performing numerical integration to calculate the area of a unit circle:

π=11111{x2+y21}dxdy×4\pi = \int_{-1}^{1} \int_{-1}^{1} \mathbb{1}_{\{x^2 + y^2 \leq 1\}} \, dx \, dy \times 4

Where

1{x2+y21}\mathbb{1}_{\{x^2 + y^2 \leq 1\}}

is the indicator function that equals 1 when a point falls inside the circle and 0 otherwise.

Interactive Simulation

Results

Total Points

0

Points Inside

0

π Approximation

Actual π: 3.141592653589793...

Simulation Speed

SlowFast