Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#USING THE IMPORT STATEMENT TO USE RANDOM,MATH AND TIME MODULE import random import math import time #ASSIGINING VALUES inside = 0 outside = 0 iterations

image text in transcribed

#USING THE IMPORT STATEMENT TO USE RANDOM,MATH AND TIME MODULE import random import math import time #ASSIGINING VALUES inside = 0 outside = 0 iterations = 1000000 # TO SPECIFY THE STARTING POINT FOR GENERATING RANDON NUMBERS random.seed(int(round(time.time()*1000))) #USING THE FOR LOOP FOR GOING THROUGH THE ITERATIONS for i in range(iterations): # select two random numbers between 0 and 1 x = random.randint(0,1) y = random.randint(0,1) # calculate distance from origin distance=math.sqrt((x*x) +(y*y)) # increment the appropriate counter if distance > 1 : outside=outside+1 else: inside=inside +1

# calculate the value of Pi total = inside+outside pi= 4*(inside/total)

# print result print (pi)

This question builds on the calculation of Pi we did in our first lab. If you did not complete the lab, or have questions about the code, you can use the solution that will be posted in Week 3. a) Modify your code or the solution (If you use the solution, you need to add comments as well) to include interaction with the user by asking the user for the number of iterations that should be performed to estimate Pi. b) Add output to the code that prints the difference between the actual value of Pi and your estimate along with the number of iterations. Formulate the output as a single string. c) Now increase the number of random numbers you use, by factors of 10, for a minimum of 5 steps. Observe what happens with the approximation of Pi. By what factor do you have to increase the number of iterations to get one more correct digit after the radix point? Keep track of the result and either plot it using Python (not necessary at this point), or by hand using a scatterplot showing the number of digits produced correctly on the y axis and the number of iterations on the x axis). This question builds on the calculation of Pi we did in our first lab. If you did not complete the lab, or have questions about the code, you can use the solution that will be posted in Week 3. a) Modify your code or the solution (If you use the solution, you need to add comments as well) to include interaction with the user by asking the user for the number of iterations that should be performed to estimate Pi. b) Add output to the code that prints the difference between the actual value of Pi and your estimate along with the number of iterations. Formulate the output as a single string. c) Now increase the number of random numbers you use, by factors of 10, for a minimum of 5 steps. Observe what happens with the approximation of Pi. By what factor do you have to increase the number of iterations to get one more correct digit after the radix point? Keep track of the result and either plot it using Python (not necessary at this point), or by hand using a scatterplot showing the number of digits produced correctly on the y axis and the number of iterations on the x axis)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago