Monte-Carlo Method
This is a small, simple example of utilising the Monte-Carlo method to calculate the value of pi. It's easy because it is only a 2-dimensional problem and thus is easy to visualise, and is trivial in its code implementation.
Picture the graph shown in the parent article - that of a quarter circle plotted on a unit x-y graph:
Below is a small example piece of Matlab code that does the above simulation:
Code:
for i = 1:1000 x = rand; y = rand; if x^2+y^2 <=1 tally = tally + 1; end count = count + 1; end area = tally/count;
This, when run on my computer, produced the result of 0.783. The area of a quarter circle works out to be π/4 if the radius is 1. π/4 = 0.7854, so you can see that this is quite close. More iterations than 1000 would produce a closer answer.