Skip to content

Instantly share code, notes, and snippets.

@kbrafford
Last active December 4, 2019 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbrafford/9cc0572809a460b7e8c851cd45be7980 to your computer and use it in GitHub Desktop.
Save kbrafford/9cc0572809a460b7e8c851cd45be7980 to your computer and use it in GitHub Desktop.
from random import uniform, seed
from math import sqrt
from multiprocessing import Pool, freeze_support
def f(count):
seed()
CIRCLES = ((10,10),(-10,10),(-10,-10), (10,-10))
tot_count, in_count = 0, 0
for _ in range(count):
x,y = uniform(-10,10), uniform(-10,10)
tot_count += 1
in_count += all([sqrt((xx-x)**2+(yy-y)**2) <= 20.0 for xx,yy in CIRCLES])
return (in_count, tot_count)
if __name__ == "__main__":
freeze_support()
POOL_SIZE, BATCH_SIZE = 23, 50000000
p = Pool(POOL_SIZE)
results = p.map(f, [BATCH_SIZE]*POOL_SIZE)
in_count, tot_count = sum([x[0] for x in results]), sum(x[1] for x in results)
print("in: ", in_count)
print("tot:", tot_count)
ratio = float(in_count)/tot_count
print("ratio:", ratio)
print("area: ", ratio * 400)
@kbrafford
Copy link
Author

Added a seed() call to each process and it gets a lot better results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment