-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8-Buffon's_needle.py
46 lines (40 loc) · 1.06 KB
/
8-Buffon's_needle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
"""Buffon's Needle.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1xw2wZVYSN0l84I0iMownXMEJFFJVkhA3
"""
import random
import math
import matplotlib.pyplot as plt
darts=[100,1000,5000,10000]
missD=[]
hitD=[]
missTheta=[]
hitTheta=[]
d=int(input("Enter Value of d:"))
l=int(input("Enter Value of l:"))
for i in darts:
print("Darts:",i)
hits=0
for j in range(i):
D=random.uniform(0,d/2)
theta=random.uniform(0,math.pi)
if D<=(l/2)*math.sin(theta):
hits=hits+1
hitD.append(D)
hitTheta.append(theta)
else:
missD.append(D)
missTheta.append(theta)
PI=((2*l)/d)*(i/hits)
print(PI)
#Scatter Plot
plt.figure(figsize=(10, 6))
plt.scatter(missTheta,missD, color= "green", marker= ".", s=80,label = "Missed Points")
plt.scatter(hitTheta,hitD, color= "red", marker= ".", s=80,label = "Hits Points")
plt.title('Buffon\'s Needle')
plt.xlabel('Values of Theta')
plt.ylabel('D')
plt.legend(loc='upper right')
plt.show()