Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import random
- from math import pi, fabs
- accuracies = {
- '10': 0,
- '100': 0,
- '1000': 0,
- '10000': 0,
- '100000': 0,
- '1000000': 0,
- '10000000': 0,
- }
- num = 0
- num_samples = [10, 100, 1000, 10000, 100000, 1000000, 10000000]
- average_val = 0
- for samples in num_samples:
- for n in range(1, 101):
- for _ in range(samples):
- x, y = random(), random()
- if x*x + y*y <= 1:
- num += 1
- average_val *= (n-1)
- average_val += 4*(float(num)/samples)
- average_val /= n
- num = 0
- 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