WARNING
Content Under Development
See
release page for
latest official PDF version.
**Chapter 1: A Simple Monte Carlo Program**
Let’s start with one of the simplest Monte Carlo (MC) programs. MC programs give a statistical
estimate of an answer, and this estimate gets more and more accurate the longer you run it. This
basic characteristic of simple programs producing noisy but ever-better answers is what MC is all
about, and it is especially good for applications like graphics where great accuracy is not needed.
As an example, let’s estimate $ \pi $. There are many ways to do this, with the Buffon Needle problem
being a classic case study. We’ll do a variation inspired by that. Suppose you have a circle
inscribed inside a square:
![Figure 1-1][fig01-1]
Now, suppose you pick random points inside the square. The fraction of those random points that end
up inside the circle should be proportional to the area of the circle. The exact fraction should in
fact be the ratio of the circle area to the square area. Fraction:
$$ \frac{Pi \cdot R^2}{(2R)^2} = \frac{Pi}{4} $$
Since the *R* cancels out, we can pick whatever is computationally convenient. Let’s go with R=1
centered at the origin:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
#include