Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- start_time = time.time()
- r = 120
- xg,yg = [], []
- def experiment(N):
- M = 0
- cnt = 0
- for _ in range(N):
- x1 = uniform(-120,120)
- x2 = uniform(-120,120)
- y1 = uniform(-120,120)
- y2 = uniform(-120,120)
- if x1**2 + y1**2 <= r**2 and x2**2 + y2**2 <= r**2:
- M += 1
- dl1 = sqrt(x1**2 + y1**2)
- dl2 = sqrt(x2**2 + y2**2)
- if dl1 < dl2:
- if dl1 >= 60:
- cnt += 1
- if N == 100000:
- xg.append(x1)
- yg.append(y1)
- else:
- if dl2 >= 60:
- cnt += 1
- if N == 100000:
- xg.append(x2)
- yg.append(y2)
- return cnt / M
- print("Ответ:",experiment(100000))
- x = [i for i in range(1000,100000,1000)]
- y = [experiment(i) for i in x]
- plt.title("Probability")
- plt.plot(x[3:],y[3:])
- plt.plot ([1, 100000],[0.5625,0.5625], 'r-')
- plt.rcParams['figure.figsize'] = (10, 6)
- plt.rcParams['figure.dpi'] = 150
- plt.show()
- plt.plot(xg,yg,linestyle="",color="red",marker="o")
- circle= plt.Circle((0,0), radius= 60)
- ax=plt.gca()
- ax.add_patch(circle)
- plt.xlim(xmin = -150,xmax = 150)
- plt.ylim(ymin = -150,ymax = 150)
- plt.rcParams['figure.figsize'] = (10, 6)
- plt.rcParams['figure.dpi'] = 150
- plt.show()
- print(f"Time:{time.time() - start_time}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement