Hey so I'm relatively new and I'm having trouble running the Monte Carlo simulation and printing the results:
import random
import math
def computePI(throws):
throws = 100
radius = 1
ontarget = 0
offtarget = 0
numthrows = 0
while throws < 10000000:
while numthrows < throws:
x = random.uniform(-1.0,1.0)
y = random.uniform(-1.0,1.0)
hyp = math.hypot(x,y)
if hyp <= radius:
ontarget += 1
numthrows+=1
else:
offtarget += 1
numthrows+=1
continue
pi = (ontarget/throws)*4
throws *= 10
return(pi)
def main ():
throws = 100
while throws <= 10000000:
difference = computePI(throws) - math.pi
print('{first} {last}'.format(first="Num =", last=throws),end = " ")
print('{first} {last}'.format(first="Calculated Pi =", last=computePI(throws)),end = " ")
if difference < 0:
print('{first} {last}'.format(first="Difference =", last=round(difference,6)))
if difference > 0:
print('{first} +{last}'.format(first="Difference =", last=round(difference,6)))
throws *= 10
main()
So I think the Monte Carlo function (computePI) is correct. I'm trying to run the Monte Carlo function for the values 100, 1000,100000,1000000, and 10000000.Is there a way to run the computePI function everytime the while loop in the main() function loops?