Empirical Lab Repository

Title: Monte Carlo Methods: Approximating PI

Author: Dave Reed, Creighton University, davereed@creighton.edu

Possible Courses: CS0, CS1

Empirical Concepts Introduced: Monte Carlo Methods, expected values, consistency, accuracy

Computer Science Concepts Used: computer-based simulation, input/output, arithmetic operations

Summary: This assignment uses a Java applet to introduce the concept of Monte Carlo methods. Using the existing Web page, students are led through a series of experiments to count the number of random dots that land inside a circle. The concepts of expected value and consistency are introduced, as students perform repeated experiments, average the results, and measure the differences between the values obtained. Once the students have collected data (the number of dots that landed inside the circle) from repeated experiments, that data is used to calculate estimates of PI based on a provided formula. Students are asked to write a simple program that takes the data from the experiments, computes an approximation of PI, and displays that approximation. The concept of accuracy and its distinction from consistency is discussed. In addition, the Law of Large Numbers is implicit (although not mentioned by name) as students perform repeated trials, noting the increase in consistency and accuracy as the number of trials increases.

As is, this assignment is heavily weighted on experimentation. Students perform numerous experiments using the provided applet, record their results, and analyze them with respect to consistency and accuracy. The programming component is relatively simple, involving only input/output and simple arithmetic operations. As such, this is an ideal assignment for the very beginning of a programming course: it utilizes simple programming skills yet shows how programs can be useful tools.

Variations: If desired, an instructor could use the idea for this assignment later in a programming course. Instead of providing the Web page with the applet, the instructor could assign the task of performing the simulation and counting the dots to the student. A program for conducting such as simulation is relatively short, but covers many of the fundamental programming constructs, including mathematical functions, conditionals, loops, and counters.