Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import random
  2. import math
  3. import sys
  4.  
  5. def main():
  6. numDarts=int(sys.argv[1])
  7. distance=float(sys.argv[2])
  8. print(montePi(numDarts,distance))
  9.  
  10. def montePi(numDarts,distance):
  11. if distance>=1:
  12. return(0)
  13. inCircle=0
  14. for I in range(numDarts):
  15. x=(2*(random.random()))-2
  16. y=random.random()
  17. d=math.sqrt(x**2+y**2)
  18. if d<=2 and d>=-2:
  19. inCircle=inCircle+1
  20. pi=inCircle/numDarts*4
  21. return pi
  22.  
  23. main()
  24.  
  25. def circle_intersection_area(num_darts, distance):
  26. if distance >= 1:
  27. return 0
  28.  
  29. in_circle = 0
  30. width = 1-distance # this is enough to cover half of the target
  31.  
  32. for i in range(num_darts):
  33. x = random.random()*width # random value from 0 to 1-distance
  34. y = random.random()*2 - 1 # random value from -1 to 1
  35. d = math.sqrt((x+distance)**2 + y**2) # distance from (-distance, 0)
  36. if d <= 1:
  37. in_circle += 1
  38.  
  39. sample_area = width * 2
  40. target_area = sample_area * (in_circle / num_darts)
  41.  
  42. return target_area * 2 # double, since we were only testing half the target
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement