Pi from Randomness — Monte Carlo

By dan • June 2, 2026 • 3 min read

# Pi from Randomness — Monte Carlo

> **Animation coming** — full Sora 2 prompt below. Video will replace this placeholder.

## Plain English first

Throw darts randomly at a square. Some land inside the largest circle that fits in the square. Count the ones inside vs total. Multiply by 4. You get pi.

This works because the area of the circle (πr²) divided by the area of the surrounding square (4r² for a 2r×2r square) equals π/4. So if you throw darts uniformly, π/4 of them should land inside the circle — and 4 times that fraction is pi.

No geometry required. Just randomness, counting, and time. With 1,000 darts you get roughly 3.1 or 3.2. With 10,000,000 darts you get 3.14159... The more darts, the more precise.

---

## Standard math notation

```
π/4 = (area of quarter-circle) / (area of unit square)
= (number of points inside circle) / (total number of random points)

Therefore:
π ≈ 4 × (points_inside / total_points)

Convergence: error shrinks roughly as 1/√n
100 points: error ≈ 0.1
10,000 points: error ≈ 0.01
1,000,000 points: error ≈ 0.001
```

---

## Verbose Python with descriptive names

```python
import random

def estimate_pi_using_random_darts(
total_number_of_darts_thrown
):
"""
Throw random darts at a 1×1 square.
Count how many land inside the quarter-circle of radius 1 centered at origin.
Pi ≈ 4 × (fraction inside circle).

Why quarter-circle? We're working in the unit square [0,1]×[0,1].
A point is inside the quarter-circle if x²+y² ≤ 1.
"""
number_of_darts_inside_circle = 0

for dart_number in range(total_number_of_darts_thrown):
random_x_position = random.uniform(0, 1)
random_y_position = random.uniform(0, 1)

distance_squared_from_origin = random_x_position**2 + random_y_position**2
point_is_inside_quarter_circle = distance_squared_from_origin <= 1.0

if point_is_inside_quarter_circle:
number_of_darts_inside_circle += 1

fraction_of_darts_inside = number_of_darts_inside_circle / total_number_of_darts_thrown
estimated_pi = 4 * fraction_of_darts_inside
return estimated_pi

# Watch the estimate improve with more darts
for number_of_darts in [100, 1_000, 10_000, 100_000, 1_000_000]:
pi_estimate = estimate_pi_using_random_darts(number_of_darts)
error = abs(pi_estimate - 3.141592653589793)
print(f"{number_of_darts:>10,} darts: π ≈ {pi_estimate:.5f} (error: {error:.5f})")
```

---

## Sora 2 video prompt

```
8-second animation. A white unit square contains a quarter-circle arc in the
bottom-left corner. Colored dots fall rapidly and randomly — blue if inside
the arc, red if outside. A live counter in the top corner updates: '4 ×
(blue/total) = 3.1...' converging toward 3.14159. By 7 seconds, thousands
of dots fill the frame. Final frame text: 'π emerges from randomness'.
Dark background, blue/red dots, clean minimal style.
```

---

## Why this works

```python
# The geometry behind it:
# Area of full circle = πr²
# Area of bounding square = (2r)² = 4r²
# Fraction: πr² / 4r² = π/4
#
# Random uniform sampling estimates area ratios.
# So (points inside) / (total points) ≈ π/4
# And 4 × that fraction ≈ π
#
# This is called a Monte Carlo method — using randomness to solve
# a problem that has nothing random about it.
# It works for any shape whose area you can check but not compute directly.
```

---

## Builds on
- [Circle Area — Why πr²?](/articles/6156ae8e-6c73-4c17-8916-8259a21cad23)
- [Pi — The Diameter That Wraps Around](/articles/pi-diameter)

## See also
- [Visual Pi Dependencies](/articles/ec3ebdf4-f6cc-47bf-b597-56fc7b53d130)
- [Visual Integrals for Programmers](/articles/01b9a7a2-d04e-4b71-b49d-82ea6c06fd04)
- [Math Foundations — Visual Table of Contents](/articles/d404884f-54fc-4289-b3f1-baaad2bec6b2)