Quiz 10: The Island of Monte Carlo

[This quiz is due at the beginning of class on Monday, November 13.]

After making it through a challenging semester in 15-462/662, you decide to take a nice long vacation on a tropical island. Unfortunately, on the way there you get caught in a storm that leaves you stranded on a deserted island with nothing but a coconut palm. To pass the time as you wait for a rescue, you decide to use your newly-acquired Monte Carlo skills to estimate the value of pi. Using a stick and a little piece of rope, you draw a perfect circle and a square around it. You then climb high up into a tree and start tossing coconuts randomly down into the square. The fraction of coconuts that lands in the circle basically tells you the value of pi (though of course you must also account for the size of the square).

Write some code that estimates the value of pi using the "coconut algorithm" described above. (You may use any programming language you like.). This code should take a single parameter, the number of coconuts, and return a single value, the estimate of pi.

double estimatePi( int nCoconuts )
{
// TODO estimate pi, and return your estimate
}