Imagine a square of side d, and a circle inscribed in it - see right
Suppose we throw darts at the square - randomly, so they hit the square 'evenly'. What fraction would lie within the circle? The area of the square is d * d The area of the circle is PI * d * d / 4
++ + +
So the probability a dart will land in the circle is PI / 4
3.13892
Program result
x 1.0
0.5
xd 0.5
1.0
In the program, we take the side of the square to be 1
We choose a random point at (x,y) The center of the circle is at (0.5, 0.5) xd is x - 0.5, and similarly for yd By Pythagoras the distance of the point from the cente the square root of xd squared plus yd squared If this is less than 0.5, the point is inside the circle. The program does this 100000 times, and displays 4 times the fraction in the circle - an estimate of PI Start Program
ke the side of the square to be 1
point at (x,y) le is at (0.5, 0.5)
stance of the point from the center is
squared plus yd squared , the point is inside the circle. s 100000 times, and displays the circle - an estimate of PI