new all

Approximate Pi (monte carlo method)


The last chapter of the book history of pi which in relation to Pierre Simon Laplace covers the monte carlo method to approximate Pi.

It is not difficult to calculate the probability of obtaining Pi correct to k decimal places in N throws. The results of such a calculation show that this method is very inefficient as far as the numerical computation of Pi is concerned; for example, the probability of obtaining Pi correct to 5 decimal places in 3,400 throws of the needle is less than 1.5%, which is very poor. Nevertheless, Laplace had discovered a powerful method of computation that did not come into its own until the advent of the electronic computer. The method that Laplace proposed consists in finding a numerical value by realising a random event many times and observing its outcome experimentally. This is today known as a Monte Carlo method.

Area of the circle = πradius2
Area of the square = 4radius2
The ratio of these two areas are:
Area of circle/Area of square = π radius2 / 4radius2 = π / 4

Based on a quarter of a square (1x1) which contains a quadrant circle with a radius of 1, plot random x and y points within that square and count those which lie within the quadrant circle.

To work out if the x,y point is within the circle, the length of the hypotenuse will be less than the radius (1).

hypotenuse= x2+ y2 (Pythagorean theorem)

π divided by 4 is equal to the total number of points inside the circle divided by total amount of points generated.
So then the value of π is 4 multipled by the total number of points inside the circle divided by the total amount of points generated

The higher the number of random x,y points generated the higher the level of accurate predictions as to the approximate value of pi.

Visualise this using d3 + javascript or click the start button:

To visualise this in python code based on 50,000 random points.

Simons blog / 2022 / 11 / 5

previous: Collected rules

next: Trying out Tailscale

new all