Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import functools
- import random
- import math
- from concurrent import futures
- def map_d(c):
- return math.hypot(random.random(), random.random())
- def reduce_d(count_inside, d):
- if d < 1:
- return count_inside + 1
- return count_inside
- def generate_random(count):
- d_list = map(map_d, range(0, count))
- count_inside = functools.reduce(reduce_d, d_list)
- return count_inside
- def main():
- count = 100000000
- cores = 4
- count_inside = 0
- with futures.ProcessPoolExecutor(cores) as executor:
- for val in executor.map(generate_random, (int(count/cores) for _ in range(cores)) ):
- count_inside += val
- print(4.0 * count_inside / count)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment