Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Python Background Info: Imagine a circle inscribed within a square that spans

ID: 3737088 • Letter: I

Question

In Python

Background Info:

Imagine a circle inscribed within a square that spans the area where -1 ? x ? 1 and -1 ? y ? 1. The area of the inscribed circle, whose radius is 1.0 would be ?.

If you were to throw darts at random locations in the square, only some of them would hit inside the circle inscribed within it. The ratio

can be estimated by the ratio

As the number of darts increases, the second ratio, above, gets closer and closer to the first ratio. Since three of the four quantities involved are known, they can be used to approximate the area of the circle — this in turn can be used to approximate ?

Functions to write

For this problem, you will write two functions (each based on loops) that use different tests for determining how many random darts to throw: forPi(n) and whilePi(error). One of the common elements of the two functions is to determine if a dart hits within the circle. Because the dart is thrown randomly, we need to use random numbers we learned earlier in the semester. You will need to import the random number package, you will also need to use the uniform() random function to determine if a pair of x and y (coordinates where the dart hits) is within the circle.

Task #1 : forPi

The forPi(n) function takes in a positive integer n and should “throw” n darts at the square. Each time a dart is thrown, this function should print

the number of darts thrown so far

the number of darts thrown so far that have hit inside the circle

the resulting estimate of ?

Return value The forPi function should return the final resulting estimate of ? after n throws.

Task #2: whilePi

The whilePi(error) function, on the other hand, will take in a positive floating-point value, error, and should then continue to throw darts at the dartboard (the square). It should only stop when the absolute difference between the estimate of ? and the real value of? is less than error.

As with the forPi function, this function should print

the number of darts thrown so far

the number of darts thrown so far that have hit inside the circle

the resulting estimate of ?

after each dart throw it makes.

Return value The whilePi function should return the number of darts thrown in order to reach the input accuracy.

Note that the whilePi function requires the actual, known value of ? in order to determine whether or not its estimate is within the error range. Although this would not be available for estimating an unknown constant, you may include the line

in your code and then use the value of math.pi as the actual value of ?.

Explanation / Answer

import random import math def forPi(n): inside=0 for i in range(0,n): a=random.uniform(-1,1) b=random.uniform(-1,1) if a*a+b*ber: total+=1 a = random.uniform(-1, 1) b = random.uniform(-1, 1) if a * a + b * b
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote