-
Notifications
You must be signed in to change notification settings - Fork 0
/
My_needlesimulation.py
59 lines (53 loc) · 1.79 KB
/
My_needlesimulation.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
47
48
49
50
51
52
53
54
55
56
57
58
59
'''Author - MD. ELIOUS ALI MONDAL
Created - 28/5/2017'''
#simulation of needle falling on a plane and estimating the value of pi
from math import *
import time
import random
start_time = time.process_time()
random.seed(5)
n = int(input('Enter the number of needles to be stimulated : '))
x1 = []
y1 = []
for i in range(n):
x1.append(random.uniform(-10,10))
x1.sort()
for i in x1:
y1.append(random.uniform(-10,10))
x2 = []
y2 = []
for i in range(len(x1)):
g = random.uniform(0,2*pi)
m = x1[i]+1/sqrt(1+tan(g)**2)
l = x1[i]-1/sqrt(1+tan(g)**2)
x2.append(random.choice([m,l]))
p = y1[i]+tan(g)/sqrt(1+tan(g)**2)
t = y1[i]-tan(g)/sqrt(1+tan(g)**2)
y2.append(random.choice([p,t]))
#Let the number of needles falling in between space = s
s = 0
import pylab
pylab.figure(1)
for i in range(-10,11):
pylab.plot([i,i],[-10,10],color = 'black')
for i in range(len(x1)):
if int(max(x1[i],x2[i])) == 0 and int(min(x1[i],x2[i])) == 0:
if (x1[i] > 0 and x2[i] > 0) or (x1[i] < 0 and x2[i] < 0):
pylab.plot((x1[i],x2[i]),(y1[i],y2[i]),color = 'blue')
s = s + 1
else:
pylab.plot((x1[i],x2[i]),(y1[i],y2[i]),color = 'red')
else:
if int(max(x1[i],x2[i]))-int(min(x1[i],x2[i])) == 0:
pylab.plot((x1[i],x2[i]),(y1[i],y2[i]),color = 'blue')
s = s + 1
else:
pylab.plot((x1[i],x2[i]),(y1[i],y2[i]),color = 'red')
end_time = time.process_time()
probability = s/n
pie = 2/(1-probability)
print('The probability of neddle falling in space between is ',probability)
print('The value of pi is ',pie)
pylab.title(('n = ',n,' Probability', probability,' Pi',pie))
print('Time taken is',end_time-start_time,'seconds')
pylab.show()