This problem is also a Monte Carlo simulation, but this time in the continuous domain: must use the following fact: a circle inscribed in a unit square
has as radius of 0.5 and an area of ?∗(0.52)=?4.π∗(0.52)=π4.
Therefore, if you generate num_trials random points in the unit square, and count how many land inside the circle, you can calculate an approximation of ?
For this problem, you must create code in python
(A) Draw the diagram of the unit square with inscribed circle and 500 random points, and calculate the value of ?
(B) Without drawing the diagram, calculate the value of ? you would get from 105 trials.
(C) After completing (B), try to get a more accurate value for ? by increasing the number of trials.The results will depend on your machine
Pi value changes as the INTERVAL is changed
import random
INTERVAL=10000
circle_points = 0
square_points = 0
for i in range(0,INTERVAL * INTERVAL):
rand_x = (random.randint(0,1000) % (INTERVAL + 1)) / INTERVAL
rand_y = (random.randint(0,1000) % (INTERVAL + 1)) / INTERVAL
origin_dist = rand_x * rand_x + rand_y * rand_y
if (origin_dist <= 1):
circle_points=circle_points+1
square_points=square_points+1
pi = (4 * circle_points) / square_points
print("Final Estimation of Pi = " + pi)
Get Answers For Free
Most questions answered within 1 hours.