Craig Horwood

Math!: Finding Pi with Random Numbers

Consider a circle of radius r, perfectly sized to fit inside a square of side length 2r.

The area of the circle is pi times r squared, whereas the area of the square is four times r squared.

The ratio of the circle's area to the square's area is therefore pi to 4.

This program generates a given number of random points in the square, and calculates how many of them are inside the circle. As the number of points generated increases, they will be evenly distributed across the square and thus approach its true area, thus the ratio of points inside the circle to the total points should approach pi to 4.

Adjust the parameters below to calculate pi:

Circle radius:
Number of random points:

Code: pi.js