Estimate \pi with darts

Imagine a circle of radius (r) inscribed inside a square with side length 2r. Now imagine throwing a dart which lands at some random location within the square (imagine you are terribly random at throwing darts). Sometimes the dart will be inside the circle and sometimes it will not. If you count the number of darts that land inside the circle and divide that by the total number of darts, that ratio ought to equal (\pi/4).

As an exercise, implement this method of estimating \pi. Write the code so that you can use any number of random darts n. Start with 1000 darts and then try larger numbers. See how close you can get to the actual value of \pi. Here are the first 100,000 digits of \pi.

Write a function called estimatepi() that takes an integer n as input, and returns as output the estimated value of \pi using n throws of the dart.

Note that this algorithm is not a very efficient or fast way of getting the digits of \pi, but it’s a fun programming exercise nevertheless and a way to get you coding.

Hint: you will need a function that generates random numbers as you should assume you are a very poor dart thrower and the location each dart hits is random (assume a uniform distribution).

Hint: to determine whether each point is inside or outside of the circle you can compute the distance between the point and the centre of the circle. If this distance is greater than the radius of the circle, it must be outside of the circle. Using the Pythagorean theorem you can easily show that the equation for the distance d between two points (x_{1},y_{1}) and (x_{2},y_{2}) is:

\begin{equation} d = \sqrt{(x_{2}-x_{1})^{2} + (y_{2}-y_{1})^{2}} \end{equation}