OU blog

Personal Blogs

Richard Walker

A Staggering Way To Estimate Pi

Visible to anyone in the world
Edited by Richard Walker, Saturday 14 March 2026 at 20:44

Being as how today is ' pi day' (date is 3/14 if you put the month number first), I thought I would present a way of estimating by experiment, rather than by calculating it using one of the many methods.

There are several ways to estimate pi by experiment. Two of the best known are Buffon's needle and Monte Carlo Integration, and before we had computers people did do physical experiments, to demonstrate these techniques really do give an estimate of pi . Today it is easier to simulate the experiments using a computer. Although these approaches only converge very slowly and are not of practical use for finding pi , they are of considerable interest from a recreational mathematics standpoint.

The idea I'm going to describe uses the concept of a random wall on the number line, in the guise of the Drunkard's Walk'.

Initially the drunkard is clinging to the lamp-post at 0 but he sets off, staggering randomly along the number line. At each stage he moved to an adjacent number, either the one to the left or the one to the right ,with equal probability. So when is at x his next position will be x minus one or x plus one , each with probability 0.5 .

Where does pi come in? Well, rather surprisingly, after a large number of such moves his expected (absolute) distance from the origin is close to Square root of two times cap n divided by pi . So if we simulate a large number of long walks and take the average of the distances each walk end up from the origin we can reverse the formula to estimate pi . Specifically if the walks are cap n moves and the average we find is a times v times g the estimate for pi is two times cap n divided by a times v times g squared ,

With the aid of a Python program you can find at the end of this post I simulated 1,000,000 random walks of 10,000 moves, and got 3.142857142857143, an error about 0.04%. That's not spectacularly close but it does support the idea that the simulation is converging to |(\pi\) as expected.

Of course you could make it a practical experiment if you wanted — just flip a coin and keep track of the number of heads vs tails. but it would be a bit tedious to do 10,000 flips

Fun Fact

Our drunkard will return to the origin with probability 1, so return is certain, but it is not possible to set a finite value for the expected number of steps this will take.

Credit to Copilot for the drunkard and lamp-post picture.

Code

import random
import math

N = 10000
moves = [-1,1]
total = 0

for trial in range (1000000):
    x = 0
    for step in range(N):
        x = x + random.choice(moves)
    total = total + abs(x)
avg = total/1000000
pi_estimate = 2*N/avg**2
print(pi_estimate)
Permalink
Share post

This blog might contain posts that are only visible to logged-in users, or where only logged-in users can comment. If you have an account on the system, please log in for full access.

Total visits to this blog: 3951376