Suppose you draw a square that has all side lengths equal to $1$. Suppose next that you draw the largest circle you can
that fits inside of the square, with the circle having its center at the center of the square. The circle will have a radius
of $1/2$. Now, the area of the square is $1^2$ or just $1$ and the area of the circle is $\pi\left(\frac{1}{2}\right)^2$ or just $\pi/4$. If you divide these two areas you'll
get $\frac{\pi}{4}$. So what?

Now suppose you put your drawing on the wall and threw darts at it. The number of darts landing in the circle, divided by those landing in the square will be the ratio of these areas or $\pi/4$! Wow! Multiply the ratio by $4$ and you have a way to find $\pi$!

But darts would be too much work...let's have the computer do it.

Let's pick two random numbers for an $(x,y)$ point on the cartesian coordinate system. To stay in the square, let's pick both $x$ and $y$ randomly to be between $0$ and $1$. This way all points will be in a square of side length $1$.

If we call the built in function `math.random()`

(without a range as discussed in this lesson), this is what we'll get...a random
number between $0$ and $1$. Try it: type a short one-line program with just `print(math.random())`

in it, and run it a few times.

Now, when an $(x,y)$ point is known, we have to see if it lands in the circle. Can you think of how to do this? Hint: What about the distance
formula which says that $d=\sqrt{(x-x_0)^2+(y-y_0)^2}$. This formula was covered in a past lesson. You can use this formula to compute how far your $(x,y)$ point is from the center of
the circle, which is at $(0.5,0.5)$. If $d<0.5$ ($0.5$=the circle's radius), then the dart must be fallen within the circle. Use and `if`

statement
to check this, and increment a counter each time this occurs.

When this is done a few (thousand) times, the ratio of your hits inside the circle and the total thrown should start to approach $\pi/4$. Neat huh?

`total=`

, `hit=`

, `d=math.sqrt( )`

and `if`

statement.
Type your code here:

See your results here:

The code will not run. Here's what needs to be fixed.

- First the total darts we want to throw needs to be defined by
the
`total=`

line (try 100, 1000, 10000 even). - Next we need a counter to count how many "hits" (or darts) fall
within the circle. To start (before we throw any darts), what should
`hit=`

be set to? - The
`d=`

statement is the hard part. You need to program in the distance formula to see how far the $(x,y)$ point is from the center of the circle which is at $(0.5,0.5)$. - Next, in the
`if`

statement, you have to check if $d$ is less than the radius of the circle.

Does your code work? Want to run it on your iPhone?

Here's your code:

- Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.
- Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into
this page
- Then click the "Use on iPhone" button that you'll see.