#### Calculating the Value of π using a Monte Carlo Method

By Charles Xie ✉

Monte Carlo methods
are a class of algorithms that uses random sampling to obtain numerical approximations to problems that might be deterministic in nature.
They are most useful when it is difficult or impossible to use other approaches.
Buffon's needle problem
is an example of using a Monte Carlo method to approximate the value of π. The following screencast video
shows a variation of the problem: If we throw a lot of dots onto a circle inscribed in a square, would the result have anything to do with π?
As shown in the video, the problem is solved using iFlow — a conceptual computing environment that we are developing to support science and engineering computation.

Click HERE to play with the above example

Let me explain the idea a bit here. As you know, the area of a circle is π*R*^{2}, where *R* is the radius.
Now, assuming that the chance of a dot hitting any point is equal everywhere within the square, the ratio between the total number of dots that fall within the circle and the total number of dots that fall
within the square in which the circle is inscribed should just be the ratio between their areas:

p = π*R*^{2}/4*R*^{2}

as the length of the square is 2*R*. This means:

π = 4p.

If we count the total number of the dots that land within the circle and the total number of the dots that land within the square, we obtain an approximate value of *p*.
Multiplying that value by four, we get an approximate value of π! True to form, the above simulation shows that the value indeed comes close to 3.14.
In general, the more dots we have thrown, the more accurate the result should be.

This kind of numerical method is often called Monte Carlo integration.