-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyneedleturtle.py
85 lines (73 loc) · 2.23 KB
/
myneedleturtle.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''Author - MD. ELIOUS ALI MONDAL
Created - 25/7/2017'''
n = int(input('Enter the number of needles to be simulated : '))
s = 0 #the number of needles falling in the space between the lines in plane
import time
import random
random.seed(5)
start_time = time.clock()
#Generating the n number of pairs
x1 = []
y1 = []
for i in range(n):
x1.append(random.uniform(-10,10))
y1.append(random.uniform(-10,10))
#setting up the turtle environmrnt
import turtle
turtle.title('my needle drop experiment')
turtle.setworldcoordinates(-11,-11,11,11)
turtle.hideturtle()
turtle.speed(0)
#setting the lines in the plane
turtle.up()
turtle.goto(-10,10)
turtle.down()
turtle.left(90)
for i in range(21):
turtle.sety(-turtle.position()[1])
turtle.up()
turtle.setx(turtle.position()[0]+1)
turtle.down()
#initialising the x2,y2 coordinate list
x2 = []
y2 = []
#putting values in x2 y2 list and throwing the needles
for i in range(n):
#moving the turtle to x1[i],yi[i]
turtle.up()
turtle.goto(x1[i],y1[i])
turtle.down()
#rotating the turtle to a random orientation
turtle.lt(random.uniform(0,360))
#moving the turtle to x2[i],y2[i]
turtle.up()
turtle.fd(1)
turtle.down()
#filling up the list of x2,y2 coordinates
x2.append(turtle.xcor())
y2.append(turtle.ycor())
#moving the turle and creating the needle(looks like randomly falling)
if int(x1[i])==0 and int(x2[i])==0:
if max(x1[i],x2[i])>0 and min(x1[i],x2[i])<0:
turtle.pencolor('red')
turtle.bk(1)
else:
turtle.pencolor('blue')
turtle.bk(1)
s = s + 1
else:
if abs(int(x1[i])-int(x2[i])) == 0:
turtle.pencolor('blue')
turtle.bk(1)
s = s + 1
else:
turtle.pencolor('red')
turtle.bk(1)
#turtle.mainloop()
#calculating the probability and value of pi
probability = s/n
pie = 2/(1-probability)
end_time = time.clock()
print('The probability of needle falling in space between is ',probability)
print('The approximate value of pi is ',pie)
print('Time taken is',end_time-start_time,'seconds')