-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomWalk.py
More file actions
114 lines (94 loc) · 2.87 KB
/
randomWalk.py
File metadata and controls
114 lines (94 loc) · 2.87 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import matplotlib.pyplot as plt
import random as ra
def axes(num):
import matplotlib.pyplot as plt
xorig = []
yorig = []
zorig = []
null_list_1 = []
null_list_2 = []
for j in range((0 - num), (0 + num + 1), 1):
xorig.append(j)
yorig.append(j)
zorig.append(j)
null_list_1.append(0)
null_list_2.append(0)
##Drawing X-Axis:
plt.plot(xorig, null_list_1, color='#c41818', linewidth=1, marker=',', markerfacecolor='blue', markersize=0, label='X-Axis')
##Drawing Y-Axis:
plt.plot(null_list_1, yorig, color='#18c431', linewidth=1, marker=',', markerfacecolor='blue', markersize=0, label='Y-Axis')
def abs1(num):
if num >= 0:
return num
elif num < 0:
return num*(0-1)
def maxmin(ls):
high = -35000000
low = 35000000
for i in range(len(ls)):
if ls[i] > high:
high = ls[i]
if ls[i] < low:
low = ls[i]
res = [high, low]
return res
def rand():
ra.seed()
dire = ra.randrange(1, 5)
if dire == 1:
dire = 'e'
elif dire == 2:
dire = 'n'
elif dire == 3:
dire = 'w'
elif dire == 4:
dire = 's'
else:
dire = None
return 0
return dire
sd = 'N'
while 1:
sd = str(input("Generate Random Walk? [Y/N]: "))
if sd == 'Y' or sd == 'y':
ls1 = []
xcoor = []
ycoor = []
xcoor.append(0)
ycoor.append(0)
ra.seed()
n = int(input("Enter the number of steps: "))
while n > 0:
direction = rand()
ls1.append(direction)
n = n-1
print(ls1)
i = 0
while i < len(ls1):
ra.seed()
if ls1[i] == 'e':
xcoor.append(xcoor[i] + 1)
ycoor.append(ycoor[i] + 0)
elif ls1[i] == 'n':
xcoor.append(xcoor[i] + 0)
ycoor.append(ycoor[i] + 1)
elif ls1[i] == 'w':
xcoor.append(xcoor[i] - 1)
ycoor.append(ycoor[i] + 0)
elif ls1[i] == 's':
xcoor.append(xcoor[i] + 0)
ycoor.append(ycoor[i] - 1)
i = i+1
max_x = maxmin(xcoor)
max_y = maxmin(ycoor)
max_2ax = maxmin([abs1(max_x[0]), abs1(max_x[1]), abs1(max_y[0]), abs1(max_y[1])])
axes(max_2ax[0])
else:
exit()
print(f'X-Axis: {xcoor}')
print(f'Y-Axis: {ycoor}')
plt.plot(xcoor, ycoor, color = 'black', linewidth = 1, marker = 'o', markerfacecolor = 'red', markersize = 3)
plt.title('Random 4-Directional Unary Walk')
plt.show()
displacement = ((xcoor[len(xcoor)-1]**2) + (ycoor[len(ycoor)-1]**2))**0.5
print(f'Net Displacement: {format(displacement, "0.2f")} units.')