As a belated celebration of Pi Day (March 14), the Petra 10th grade Computer Science class recently completed a programming lab to calculate the value of π by using the Monte Carlo method. In addition to learning important coding structures, the class also reviewed how to graphically represent data and how to evaluate the efficiency of their program.
They also learned the vital lesson that finding a right solution to a problem isn’t the same as finding the best solution.
The Monte Carlo Method
The Monte Carlo method of approximating the value of π begins with a circle inscribed in a square. The circle has a radius of 1 unit, which means the sides of the square are 2 units long.
The area of the circle is π square units (A = π * 12), and the area of the square is 4 square units (A = 22). This means that the ratio of the area of the circle to the area of the square is π : 4 or π / 4.
If we were to throw a certain number of darts at the circle, and then count the number of darts that hit the circle, we can approximate the value of π. The ratio of number of hits (H) to the total number of darts thrown (D) is approximately equal to the ratio of the areas: H / D ≈ π / 4.
If we divide both side by 4, we find a formula for approximating π: π ≈ (H / D) / 4. Now we just need to write a computer program that can simulate the dart throwing.
Throwing the Darts
The Python programming language that the Petra students are learning has a very useful graphics module called Turtle. Using the Turtle module and the approximation formula in the section above, we were able to write a program that simulated random dart throwing.
The 1-unit scale of the target prohibited us from drawing a true circle, so we added some code to mark hits in red and misses in blue. Then, we began testing our program, starting with 10 darts.
Since 10 darts gave us a very poor approximation of 2.8, we then tried 100 darts.
Increasing our dart throws by an order of 10 gave us more accuracy, so we ran tests of 1000 darts and 10,000 darts.
Throwing 10000 darts increased our accuracy, but we also noticed that the program took significantly longer to run. We ran one more test of 100,000 darts, which gave us some significant data to consider.
The final test with 100,000 darts took 20 minutes to complete on my MacBook Pro without a significant gain in accuracy. After completing this test, we assessed how helpful our program was.
The final test of 100,000 darts gave us clear evidence that the Monte Carlo method for approximating π is not very effective. 100,000 iterations give us a very imprecise result and takes a lot of time to do it. Though removing the graphical element from our program would certainly speed up our tests, we realized that other approximation methods (like Archimedes’ method) would be faster and more efficient.
First, even if the first solution works, we should keep looking for a better one. Our initial program worked just fine, but it required too many resources to justify its use. To find a better solution meant starting again with a better tool for approximation.
Second, failure can help lead us to the best solution. We spent a lot of time building our program, only to realize that it wasn’t a useful solution. It would be easy to think that our time was wasted and become discouraged. But our initial failure actually helped us realize a better solution.
Perhaps the most important lesson this lab taught us was that failure is a necessary part of learning, problem solving, and life in general.
Or as is commonly said in the world of digital making, the word fail is an acronym for First Attempt In Learning.