Question: Program Specifics: This program estimates pi using random numbers. The accuracy of the estimate increases as the number of random points are used. Create a

 Program Specifics: This program estimates pi using random numbers. The accuracyof the estimate increases as the number of random points are used.

Program Specifics: This program estimates pi using random numbers. The accuracy of the estimate increases as the number of random points are used. Create a routine that takes the number of iterations to do, and loops that many times. Each iteration it uses random.random() twice to create a random location with x and y between 0 and 1, and keeps track of the number of times that the location is within a [quarter arc of a] circle of radius 1 (aka x^2 + y^2 is no larger than 1^2) Finally, it returns the number of times it was within the arc, divided by the total number of iterations and then multiplied by 4 (since we are actually only looking at the upper left quarter of the circle we have to multiply by 4) FYI - This method is called the Monte Carlo method 1 2 3 4 5 # This program estimates pi using random numbers. The accuracy of 6 # the estimate increases as the number of random points are used. 7 # 8 # Create a routine that takes the number of iterations to do, and loops 9 9 # that many times. 10 # # # 11 # # Each iteration it uses random.random twice to create a random 12 # 10 location with x and y between 0 and 1, and keeps track of the number of 13 times that the location is within a quarter arc of a] circle of radius 1 14 # 15 # Caka x^2 + y^2 is no larger than 1^2) 16 17 # Finally, it returns the number of times it was within the arc, divided 18 # by the total number of iterations and then multiplied by 4 19 # # 20 # (since we are actually only looking at the upper left quarter of the 21 # circle we have to multiply by 4) 22 # 23 # This method is called the monte carlo method 24 # 25 26 import random 27 28 ... insert your code here 29 30 random.seed(1) # Force the random to be not-so-random 31 32 iterations = input("# of iterations: ") 33 iterations = int(iterations) 34 35 print("") 36 37 result = compute_pi(iterations) 38 print(iterations, "iterations computed pi to be", result) 39 40

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!