Guest User

Untitled

a guest
Oct 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. import functools
  2. import random
  3. import math
  4. from concurrent import futures
  5.  
  6.  
  7. def map_d(c):
  8. return math.hypot(random.random(), random.random())
  9.  
  10.  
  11. def reduce_d(count_inside, d):
  12. if d < 1:
  13. return count_inside + 1
  14.  
  15. return count_inside
  16.  
  17.  
  18. def generate_random(count):
  19. d_list = map(map_d, range(0, count))
  20. count_inside = functools.reduce(reduce_d, d_list)
  21. return count_inside
  22.  
  23.  
  24. def main():
  25. count = 100000000
  26. cores = 4
  27. count_inside = 0
  28.  
  29. with futures.ProcessPoolExecutor(cores) as executor:
  30. for val in executor.map(generate_random, (int(count/cores) for _ in range(cores)) ):
  31. count_inside += val
  32.  
  33. print(4.0 * count_inside / count)
  34.  
  35.  
  36. if __name__ == '__main__':
  37. main()
Add Comment
Please, Sign In to add comment