[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]


The ProblemThere should be some long story here about how and why the king ordered that lots of cannonballs be fired into a field just containing a circular pond. But I'm a programmer, not a storyteller  so the essence is this: Assign points randomly inside a square area that just contains a circular area, and the ratio of hits in the circle to total shots. It must be related somehow to Pi. [Pi, by definition is the ratio of the distance around a circle (circumference) to the distance across (diameter).] Background & TechniquesIf we assume that the square is 1 mile on a side and the pond is 1 mile across, then the area of the square is 1 square mile. The radius of the pond is 1/2 mile and its area is Pi*R^{2 }or 0.25Pi. If the shots are uniformly distributed over the area of the field then the ratio of shots in the pond to shots in the field is approximately the same as the ratio of the two areas (.25Pi/1 or just Pi/4). So the best estimate of Pi is simply 4 times this ratio. The plan is to generate random numbers inside the field, and increment the inpond count if it is within 1/2 mile of the center of the pond. We can use the Pythagorean theorem to calculate the distance of our random points from the center of the field. Computationally, we use the randomize function to start generating random numbers with different random values for each run. Without randomize, the numbers generated will be random (actually called pseudorandom), but would be repeatable from run to run. The random function returns a random number between 0 and 1 if called without parameters. If we assume a coordinate system with origin at the center the field, we can subtract 1/2 from the random points and end up up x and y values between 1/2 and +1/2. By the Pythagorean theorem for distance, x^{2}+y^{2}=d^{2}, so the point is in the pond if x^{2}+y^{2}<.25. That's about it. I initialized the two count fields using the constant declaration feature of Delphi, added a reset button to reset the counters at any time. This allows multiple clicks on the Shoot! button to produce cumulative results  we should be able to see the estimates get close to Pi as the number of shots increases. Exploring the Program
Suggestions for Further StudyIt seems like graphics would be nice to show the cannonballs, the problem is that after a few thousand shots, the pond and the field are covered with canon balls and not much is seen (also graphics would slow the program down by a lot). So, having tried it, I can't recommend graphics for this app. Pi is an interesting number with lots of literature on the Web about it. There are other techniques that converge to the true value of Pi much faster than the one presented here. Another program in this set, uses Buffon's Needles technique to estimate Pi. Many series converge to some value related to Pi, for example pi /4 = 1  1/3 + 1/5  1/7 + .... or pi/2 = 2/1 x 2/3 x 4/3 x 4/5 x 6/5 x 6/7 x 8/7 x 8/9 ... I wonder which series converges fastest? Or if that is even known? 
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 20002018, Gary Darby All rights reserved. 