|
Re: Math Quiz 9
My simple Python Monte Carlo script gave me an average of 0.521408 (or 0.52141 rounded to 5 digits) in ~5e9 iterations (10 miles of hiking worth).
Code is below:
Code:
import numpy as np
iterations = 10000000
avg = 0
for i in range(100000):
for i in range(iterations):
pos = np.random.rand(4)
length = np.sqrt((pos[0]-pos[1])**2+(pos[2]-pos[3])**2)
avg = (avg*i + length)/(i+1)
with open("test.txt", "a") as f:
f.write(str(avg)+'\n')
Code:
import numpy as np
f = np.loadtxt('test.txt')
print(np.average(f))
print(len(f))
I split it into two scripts just so I could stop the computation whenever without altering the result.
Edit: Reps to whoever finds the bug in my code and explains what it does.
__________________
2012 Utah Regional Rookie All-Star
2013 Phoenix Regional Judge's Award for "design process and prototyping"
2014 Hub City Regional Quality Award, Arizona Regional Excellence in Engineering Award
2015 Arizona East Regional Creativity Award, Winner
2016 Arizona North Regional Finalist, Arizona West Excellence in Engineering Award, Finalist
Last edited by z_beeblebrox : 16-07-2016 at 22:57.
Reason: New challenge
|