Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import random
  2. import math
  3. import matplotlib
  4. import matplotlib.pyplot as plt
  5. #%matplotlib inline
  6.  
  7. def get_random_point():
  8. while True:
  9. x = random.uniform(-1, 1)
  10. y = random.uniform(-1, 1)
  11. if math.sqrt(x ** 2 + y ** 2) < 1:
  12. return (x, y)
  13.  
  14. def condition(N):
  15. n = 0
  16. N1 = 0
  17. for i in range(N):
  18. x1, y1 = get_random_point()
  19. x2, y2 = get_random_point()
  20. length = math.sqrt((x2-x1)**2+(y2-y1)**2)
  21. if ((x1**2 + y1**2) <= 1) and ((x2**2 + y2**2) <= 1):
  22. N1 += 1
  23. if length < 1:
  24. n += 1
  25. return n/N1
  26. N = 100000
  27. print("Answer:")
  28. print(condition(N))
  29.  
  30. #x0 = [i for i in range(1, 20000, 100)]
  31. #y0 = []
  32.  
  33. #for i in range(1, 20000, 100):
  34. # y0.append(condition3(i))
  35.  
  36.  
  37. #plt.plot(x0[2:], y0[2:])
  38. #plt.title('PLOT')
  39. #plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement