Guest User

montepi

a guest
Feb 12th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. from random import random
  2.  
  3. from math import pi, fabs
  4.  
  5. accuracies = {
  6. '10': 0,
  7. '100': 0,
  8. '1000': 0,
  9. '10000': 0,
  10. '100000': 0,
  11. '1000000': 0,
  12. '10000000': 0,
  13. }
  14.  
  15. num = 0
  16. num_samples = [10, 100, 1000, 10000, 100000, 1000000, 10000000]
  17.  
  18. average_val = 0
  19.  
  20. for samples in num_samples:
  21. for n in range(1, 101):
  22. for _ in range(samples):
  23. x, y = random(), random()
  24. if x*x + y*y <= 1:
  25. num += 1
  26. average_val *= (n-1)
  27. average_val += 4*(float(num)/samples)
  28. average_val /= n
  29. num = 0
  30.  
  31. print 'With ' + str(samples) + ' samples we have an error margin of ' + str(fabs(average_val - pi))
Advertisement
Add Comment
Please, Sign In to add comment