Last active
December 4, 2019 03:38
-
-
Save kbrafford/9cc0572809a460b7e8c851cd45be7980 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added a seed() call to each process and it gets a lot better results