(c) 2021 George Gadanidis

Get the Task as a PDF: https://learnx.ca/wp-content/uploads/2021/04/estimating-pi-python-task.pdf

Estimate the value of Pi using the number of random (x, y) points (or “darts”) that fall in the biggest circle that fits in a 200 x 200 square in the centre of the coordinate system.

Start by coding these components:

- Generate 1000 (x, y) points with x and y being random numbers from -100 to 100.
- Count the number of points that fall in the circle and the number of points that fall outside of the circle.
- For each point calculate use this information to estimate the value of Pi
- Plot all of the values of Pi, as shown below

Create a poster presentation of what you did, what you learned, and what else you’d like to know.

NOTE: A possible solution: https://colab.research.google.com/drive/1LlDFv6YYknVZTnpX6alsG1UMbaIb6dXN#scrollTo=uyaOjPbZmxuq

How can we write code to dynamically model the square-circle relationship and use the relationship to estimate Pi?

**1. In search of Pi**

Before we start coding, we need to understand the math relationship that gives us Pi. Here is one way:

We have a pink circle, with radius **r**, that just fits inside a blue square. That is, the pink circle is the biggest circle that can fit inside the square.

**A.** What is the area of the circle? ___________

**B. **What is the area of the square? ___________

**C.** Complete the fraction below, and simply expressions as much as possible:

**2. Connecting the dots**

Discuss in your team:

Suppose we have these 3 variables we can use to count the number of pink dots and the number of blue dots, and then estimate the value of Pi:

- pinkCounter
- blueCounter
- piEstimate

**A.** How may we use these variables, and the result from 1.C, to estimate the value of Pi?

**B.** Write an algebraic expression using these variables to estimate the value of Pi.

** 1. 100 random (x, y) points**

The code on the right plots 100 random (x, y) points, with x & y from -100 to 100.

- Enter and run the code

- Change the value of darts to 10

- What is the effect?

- For the code below, change 200 to 20 and 100 to 10

- What is the effect?

- For the code below, change ‘b.’ to ‘ro’

- What is the effect?

**2. Which points are outside the circle? **

We have a 200 x 200 pixel square, with (0,0) in its centre.

What is the radius of the largest circle the fits in the square?

If a point is on the circumference of the circle then, using the Pythagorean Theorem, we know the x^{2} + y^{2} = 100^{2}.

What does the Python code below do?

Use the variables **dartsInCircle** and **dartsInSquare** to write an expression that calculates an estimate of Pi. See the Mindset section for help.