daily pastebin goal
43%
SHARE
TWEET

Untitled

a guest Jun 26th, 2013 40 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import numpy
  3. from numpy.random import rand
  4. import sys
  5. import multiprocessing
  6. from numexpr import evaluate
  7.  
  8. if len(sys.argv) < 3:
  9.     workers = 4
  10. else:
  11.     workers = int(sys.argv[2])
  12. if len(sys.argv) < 2:
  13.     needleCount = 30000000
  14. else:
  15.     needleCount = int(sys.argv[1])
  16.  
  17. def generateNeedles(n):
  18.     cross = 0
  19.     pi_x = numpy.pi/2.0
  20.     i = 0
  21.     k = 500000
  22.     while n:
  23.         if n < k:
  24.             k = n
  25.         n -= k
  26.  
  27.         r = rand(k+1)
  28.         x = r[:-1]
  29.         ct = r[1:]
  30.         #ct = evaluate('cos(ct * pi_x) * .25')
  31.         #cross += evaluate('(x >= 1-ct) | (x <= ct)').sum()
  32.         cross += evaluate('(x >= 1 - cos(ct * pi_x) * .25) |'
  33.                           '(x <= cos(ct * pi_x) * .25)').sum()
  34.        
  35.     return cross
  36.  
  37.  
  38. def splitLoad(n, k):
  39.     load, rest = divmod(n, k)
  40.     loads = [load] * k
  41.     loads[0] += rest
  42.  
  43.     cross = multiprocessing.Pool(k).map(generateNeedles, loads)
  44.     return sum(cross)
  45.        
  46.  
  47. if __name__ == '__main__':
  48.     cross = splitLoad(needleCount, workers)
  49.     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