CIS 1.5 Science Section

Brooklyn College

Professor Langsam

 

Assignment #3

 

Monte Carlo Methods and p[1]

 

Monte Carlo methods use random number generators in simulations. For example, one might study traffic flow by using a random number generator to specify when, where, and how fast vehicles are moving. One might study the behavior of the stock market by using a random number generator to simulate when and how much of a stock an investor is buying or selling. Monte Carlo methods are especially valuable when more conventional approaches (e.g., solving differential equations) are very difficult or impossible to use. They can often be used (as in the traffic flow example) to directly simulate a physical situation.

 

One of the simplest applications of Monte Carlo methods is to estimate p, the ratio of the area of a circle to the square of its radius. To see how this works imagine that you have a circular dart board with a radius of 1 foot, and the dartboard is attached to a square with sides 2 feet in length so that the edge of the dartboard is inscribed in the square. Now close your eyes and start throwing darts at the square. After (say) several thousand tosses, you open your eyes and count the number of darts that hit the dartboard and the number that hit the square.

 

           

      2 feet

Think about the ratio

 

 

Since the darts were tossed randomly at the board, we would guess that this ratio should be about the same as

 

 

So suppose

 

n = Number that hit the square + Number that hit the dartboard

d = Number that hit the dartboard.

 

Then since the area of the square is 4 square feet and the area of the circle is p square feet, we should have

 

Equivalently, we should have

 

 

Furthermore, as n, the total number of darts hitting the square and the dartboard, increases, the law of averages says that the approximation should get better and better.

 

We can use this idea to estimate _ by replacing our dartboard and dart-throwing with a random number generator. Here’s the basic idea:

 

in_circle_count = 0;

seed the random number generator;

for (i = 0; i < n; i++) {

Generate random x-coordinate in [-1,1];

Generate random y-coordinate in [-1,1];

if ((x,y) is in the unit circle)

in_circle_count++;

}

pi_estimate = 4.0*in_circle_count/n;

 

 

The C++ cstdlib library contains a random number generator, rand(), which will generate a random number between 0 and RAND_MAX, RAND_MAX being a value defined in the library as well. Random number generators are ordinarily “seeded” to control the start of the sequence of random numbers. So if a program is supposed to generate a different sequence of random numbers every time it’s run, the program could be seeded with something like the number of seconds since midnight. A discussion of random  numbers is beyond the scope of this assignment. The interested student should refer to one of the references listed in the footnotes[2],[3]

 

We desire a random number between [-1, 1]. The following formula will generate such a number.

 

x = pow(-1.0, rand() % 2) *

rand()/(double(RAND_MAX)+1);

           

We can determine whether (x, y) is in the unit circle by finding out how far it is from

(0, 0), if

 

 

 

We can save a little bit of computational work by observing this is true, if and only if

 

 

Write a C++ program to implement the Monte Carlo Method for the Estimation of p. Your input should be the number of (x,y) pairs (“darts”). Run the program with increasingly large values of n until the value you obtain for p approaches 3.1415.

 

Questions:

1.      How large of an n is required to approach this value?

2.      How long does it take your computer to compute this value? Investigate the use of the time function in the C++ ctime library[4].

3.      What are the problems with the random number generator that we have used? Investigate other method of producing random number generators.

 

 

Be sure your program is clearly documented, well structured and uses meaningful variables. Along with your program and your results, submit a brief report that discusses the above questions.



[1] www.cs.usfca.edu/peter/cs220/p3.pdf

[2] http://members.cox.net/srice1/random/crandom.html

[3] http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html

[4] http://www.cplusplus.com/reference/clibrary/ctime/