Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- import matplotlib
- import matplotlib.pyplot as plt
- #%matplotlib inline
- def get_random_point():
- while True:
- x = random.uniform(-1, 1)
- y = random.uniform(-1, 1)
- if math.sqrt(x ** 2 + y ** 2) < 1:
- return (x, y)
- def condition(N):
- n = 0
- N1 = 0
- for i in range(N):
- x1, y1 = get_random_point()
- x2, y2 = get_random_point()
- length = math.sqrt((x2-x1)**2+(y2-y1)**2)
- if ((x1**2 + y1**2) <= 1) and ((x2**2 + y2**2) <= 1):
- N1 += 1
- if length < 1:
- n += 1
- return n/N1
- N = 100000
- print("Answer:")
- print(condition(N))
- #x0 = [i for i in range(1, 20000, 100)]
- #y0 = []
- #for i in range(1, 20000, 100):
- # y0.append(condition3(i))
- #plt.plot(x0[2:], y0[2:])
- #plt.title('PLOT')
- #plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement