← back to the garden

seedling

Monte Carlo Pi: Learning Through Randomness

planted · October 24, 2025
#monte-carlo#distributed-computing#rust#webassembly#algorithms#mathematics

The Idea

How do you calculate something as precise as pi using nothing but randomness?

The Thought Experiment

Imagine a dartboard:

  • A perfect circle inside a square
  • You throw darts randomly at the square
  • Some hit inside the circle, some outside
  • The ratio reveals pi

That's it. That's the entire method.

The Geometry

Setup:

  • Circle with radius 1 (area = pi x 1 squared = pi)
  • Square with side 2 (area = 2 x 2 = 4)
  • Ratio of areas = pi/4

The insight: If we throw darts randomly, the ratio of hits should match the ratio of areas.

Darts in circle / Total darts ≈ π/4

Therefore: π ≈ 4 × (Darts in circle / Total darts)

Why This Matters

This simple experiment is useful for learning distributed computing:

Parallelizable: Every dart throw is independent. A thousand browsers can throw darts simultaneously without coordinating.

Scalable accuracy: Want more precision? Throw more darts. The law of large numbers guarantees convergence.

Verifiable: We know what pi should be. Perfect for testing a distributed system.

The Implementation Path

Step 1: Random Point Generation

// Generate random x, y coordinates between -1 and 1
let x = random_float();
let y = random_float();

Step 2: Circle Test

// Check if point is inside the unit circle
if x * x + y * y <= 1.0 {
    inside_circle += 1;
}

Step 3: Estimate

// After N iterations
let pi_estimate = 4.0 * (inside_circle as f64) / (total_points as f64);

That's the entire algorithm.

Convergence

With 100 darts: pi ≈ 3.2 (rough approximation) With 10,000 darts: pi ≈ 3.14 (getting closer) With 1,000,000 darts: pi ≈ 3.14159 (pretty accurate) With 1,000,000,000 darts: pi ≈ 3.141592653 (very accurate)

The convergence is slow (square root of N), but that's okay. More compute = better results.

Why It's Perfect for Distributed Computing

Independent workers: Browser A throws 1 million darts. Browser B throws 1 million darts. Both work independently.

Simple aggregation: Just combine the counts:

total_inside = browser_a_inside + browser_b_inside + ...
total_points = browser_a_points + browser_b_points + ...
pi = 4 * total_inside / total_points

No data transfer: Each browser just sends back two numbers: hits inside, total throws.

Fault tolerant: If a browser disconnects, we just use what we got. No corruption, no complex recovery.

What This Teaches

Building this covers everything you need for distributed computing:

  1. Computation kernel (the dart throwing logic)
  2. Work distribution (how many darts per browser?)
  3. Result aggregation (combining answers)
  4. Validation (does our answer make sense?)

The same pattern applies to financial modeling, physics simulations, machine learning sampling, and optimization problems.

Current Status

Seedling stage: Understanding the algorithm and implementation approach.

Next milestone: Write the Rust/WASM implementation and test in a single browser.

Then: Add multiple browsers, measure speedup, validate the distributed architecture.