Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. from random import random
  2. from operator import add
  3.  
  4.  
  5. NUMBER_OF_STEPS_FACTOR = 100000
  6.  
  7.  
  8. def f(_):
  9. x = random() * 2 - 1
  10. y = random() * 2 - 1
  11. return 1 if x ** 2 + y ** 2 <= 1 else 0
  12.  
  13.  
  14. def run(spark, config):
  15. number_of_steps = config['partitions'] * NUMBER_OF_STEPS_FACTOR
  16. count = spark.sparkContext\
  17. .parallelize(range(1, number_of_steps + 1),
  18. config['partitions']).map(f).reduce(add)
  19. return 4.0 * count / number_of_steps
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement