We can use millions of random numbers to calculate Pi. This is the Monte Carlo simulation approach and is so called because it refers to the randomness inherent in casinos for which Monte Carlo is famous. This is a neat little trick and is great exercise for A level students.
We start off with a quarter circle with a radius of 1 unit. We then draw a square with an area of 1 by 1 unit that encompasses the quarter circle. We know that the area of a circle, a is Pi * r * r. Given that r=1 in this case, then a is the same as Pi. Millions of points from randomly selected pairs of x and y coordinates within the square are selected. Using Pythagoras where the magnitude of a point is given by sqrt(x*x + y*y) we can determine whether a point falls inside our outside the perimeter of the circle. The radius of the circle is 1 so if the point has a magnitude of less than 1 we know that it lies within the circle. Based on the proportion of points that fall within the perimeter of the circle we can determine the area of the quarter circle which has an area a/4. Given that a=Pi, then we multiply the proportion of points that fall within the perimeter of the circle by 4 and this gives us Pi. The more points we select the better our estimate of Pi but the longer simulation takes to run. The pseudocode is given here:
import random
import math
SAMPLES=4000000
total=0
for i in range(SAMPLES):
x=random.random()
y=random.random()
z=math.sqrt(x*x+y*y)
if z<=1:
total=total+1
print(total/SAMPLES*4)
We start off with a quarter circle with a radius of 1 unit. We then draw a square with an area of 1 by 1 unit that encompasses the quarter circle. We know that the area of a circle, a is Pi * r * r. Given that r=1 in this case, then a is the same as Pi. Millions of points from randomly selected pairs of x and y coordinates within the square are selected. Using Pythagoras where the magnitude of a point is given by sqrt(x*x + y*y) we can determine whether a point falls inside our outside the perimeter of the circle. The radius of the circle is 1 so if the point has a magnitude of less than 1 we know that it lies within the circle. Based on the proportion of points that fall within the perimeter of the circle we can determine the area of the quarter circle which has an area a/4. Given that a=Pi, then we multiply the proportion of points that fall within the perimeter of the circle by 4 and this gives us Pi. The more points we select the better our estimate of Pi but the longer simulation takes to run. The pseudocode is given here:
- Select random x value between 0 and 1
- Select random y value between 0 and 1
- Calculate Pythagoras: sqrt(x*x+y*y)
- If this value is 1 or less then it is inside the (quarter) circle otherwise it is outside.
- Run this millions of times and calculate the proportion of points that landed within the circle.
- Multiply the value by 4 and this will be Pi.
import random
import math
SAMPLES=4000000
total=0
for i in range(SAMPLES):
x=random.random()
y=random.random()
z=math.sqrt(x*x+y*y)
if z<=1:
total=total+1
print(total/SAMPLES*4)
Comments
Post a Comment