# CPSC150AScientific Computing

## Assignment 3

Monte Carlo Estimation

Earlier this semester we estimated pi using the Leibniz approximation. Another way of estimating the value of pi is to use Monte Carlo Estimation. The technique is named after the casino because it depends on random chance and probability.

To estimate the area of a circle using Monte Carlo Estimation, put a circle inside of a rectangle. Then generate many random points inside of the rectangle and count the number of points that are inside the circle. The number of points that are inside, divided by the total number of points is an approximation of the circle’s fraction of the rectangle’s area. Use the area of the rectangle and the circle’s fraction of the rectangle’s area to compute the approximate area of the circle. Finally, use the equation for the area of a circle, a = π r2, to approximate pi.

#### Details

Create a program that visualizes Monte Carlo Estimation of pi. The program should draw a circle and a square, larger than the circle, both centered in the window using turtle graphics. It should then use a for loop and the random module to draw many points located anywhere inside of the square. Each point should be drawn as a red dot if it is outside of the circle, or a green dot if it is inside of the circle.

A point is inside of a circle if the distance between the point and the center of the circle is less than or equal to the radius of the circle.

The distance between two points can be computed using the Euclidean Distance formula:

$$d = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$$

Where d is the distance between the points (x1, y1) and (x2, y2).

The program should also count the number of points that are inside the circle. Once the program finishes drawing points, it should use the fraction of points that are inside the circle to print the estimated area and the estimated value of pi.

The assignment will be graded on the following requirements according to the course’s programming assignment rubric.

A functional program will:

• draw a circle centered in a larger square that is centered in the window
• draw many dots at random locations potentially anywhere in the square, but not outside the square
• draw dots inside the circle green and dots outside the circle red
• count the number of dots inside the circle
• print an estimate of pi using the number of dots inside the circle

### Submission

Submit your program as a .py file on the course Inquire page before class on Tuesday February 23rd.