Ancient CS 61 Content Warning!!!!!1!!!
This is not the current version of the class.
This site was automatically translated from a wiki. The translation may have introduced mistakes (and the content might have been wrong to begin with).

Monte Carlo Simulation

We actually provide all the code that implements the Monte Carlo simulation, but in case you're interested. Here is what we're doing.

Monte Carlo simulation is named after the city in Monaco, where the primary attractions are casinos that have games of chance. Gambling games, like roulette, dice, and slot machines, exhibit random behavior. It is an estimation technique that uses randomness to produce estimates of potentially hard to measure values.

For example, consider the problem of computing the ratio of the circumference of a circle to its diameter (that would be pi). If you have taken trigonometry or calculus, then you may know how to compute an approximate value for by any one of several means, but for the time being let's suppose that you do not know much math, but have a geometry book you can consult. Glancing quickly through the geometry book, you find the following useful bits of information:

Therefore, the ratio of the area of the circle to the area of the square is going to be (pi)r^2/4r^2 which reduces to pi/4. So, if we could determine this ratio, we could compute pi.

MonteCarlo.png

What if we did the following: generate a bunch of points inside the square -- that would require generating random coordinates for x and y such that both values range from [-1, 1]. For each value, we can determine if the point lies in the circle using the equation above. If we generate N points and C of them are on the circle, then we know that C/N is equal to pi/4 and pi is equal to 4(C/N).

So, we are using randomness (the generation of points in the square) to produce an estimate of the value of pi. You may be quite surprised how accurate a value we can get with relatively few points. You'll see how well our Uber passengers do!