daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Jun 25th, 2013 35 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. from math import cos, pi
  3. from random import random
  4. import sys
  5. import multiprocessing
  6.  
  7. workers = 4
  8. needleCount = 30000000
  9.  
  10. def generateNeedles(n):
  11.     cross = 0
  12.     pi_05 = pi/2.0
  13.     for i in xrange(n):
  14.         x = random()
  15.         cos_theta_025 = cos(random() * pi_05) * 0.25
  16.         cross += x + cos_theta_025 >= 1 or x <= cos_theta_025
  17.     return cross
  18.  
  19.  
  20. def splitLoad(n, k):
  21.     load, rest = divmod(n, k)
  22.     loads = [load] * k
  23.     loads[0] += rest
  24.  
  25.     cross = multiprocessing.Pool(k).map(generateNeedles, loads)
  26.     return sum(cross)
  27.  
  28. if __name__ == '__main__':
  29.     cross = splitLoad(needleCount, workers)
  30.     print cross, needleCount, needleCount / float(cross)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top