Question: ** Python ** The value of Pi can be estimated by repeatedly choosing random locations (uniform distribution, between 0 and 1) within a unit square
** Python **
The value of Pi can be estimated by repeatedly choosing random locations (uniform distribution, between 0 and 1) within a unit square (sides of length 1) and checking to see how many locations fall inside the unit circle touching the edges of the square (radius 0.5). Given that the area of a circle is Pi times the square of the radius, and that the area of the square is the square of the length, we can calculate the value of Pi by counting the number of points that fall inside and outside the circle (but within the square).
- Open up the file calculatePi (posted below) and add the required code as indicated. It is not acceptable to start with a different file, you need to follow the format given in the file.
- Run the code and verify that you get a value (roughly) close to Pi.
- Submit the file for Part 3 with your commented code (1.5 marks)
Some hints: Sometimes the result can be off by a factor of 4. Think about how that connects to the range of random numbers you use. There are also some explanations that use different limits of the random numbers, but for this lab, it's easiest if you create numbers between 0 and 1.
Following is the calculatePi.py code
import random import math import time
inside = 0 outside = 0 iterations = 1000000
INSERT CODE TO SEED THE RANDOM NUMBER GENERATOR HERE for i in range(iterations): # select two random numbers between 0 and 1 x = INSERT CODE HERE y = INSERT CODE HERE # calculate distance from origin INSERT CODE HERE # increment the appropriate counter if INSERT CONDITION : INSERT CODE HERE else: INSERT CODE HERE
# calculate the value of Pi INSERT CODE HERE
# print result INSERT CODE HERE
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
