Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- import sys
- def main():
- numDarts=int(sys.argv[1])
- distance=float(sys.argv[2])
- print(montePi(numDarts,distance))
- def montePi(numDarts,distance):
- if distance>=1:
- return(0)
- inCircle=0
- for I in range(numDarts):
- x=(2*(random.random()))-2
- y=random.random()
- d=math.sqrt(x**2+y**2)
- if d<=2 and d>=-2:
- inCircle=inCircle+1
- pi=inCircle/numDarts*4
- return pi
- main()
- def circle_intersection_area(num_darts, distance):
- if distance >= 1:
- return 0
- in_circle = 0
- width = 1-distance # this is enough to cover half of the target
- for i in range(num_darts):
- x = random.random()*width # random value from 0 to 1-distance
- y = random.random()*2 - 1 # random value from -1 to 1
- d = math.sqrt((x+distance)**2 + y**2) # distance from (-distance, 0)
- if d <= 1:
- in_circle += 1
- sample_area = width * 2
- target_area = sample_area * (in_circle / num_darts)
- return target_area * 2 # double, since we were only testing half the target
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement