-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomWalk.py
73 lines (48 loc) · 1.37 KB
/
RandomWalk.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
import numpy as np
import matplotlib.pyplot as plt
import random
import matplotlib.animation as animation
N=1000
values= np.zeros([N,N])
Possible_Steps=['up','down','right', 'left']
path=[]
walk=[]
The_Drunk=np.array([0,0])
random.seed(1)
#for amount of steps(N), either +/- 1 to I or J, but <x or y edge
#else choose another direction
# and keep track so I know it's path
for i in range (N):
motion=random.choice(Possible_Steps)
path.append(motion)
for i in path:
walk.append(np.copy(The_Drunk))
#print(The_Drunk)
#print(i)
if i == 'up':
The_Drunk[0]+=1
if i == 'down':
The_Drunk[0]-=1
if i == 'left':
The_Drunk[1]-=1
if i =='right':
The_Drunk[1]+=1
walk = np.transpose(np.array(walk))
#rint('the walk in an array:',walk)
print('x positions', walk[0])
print('y positions', walk[1])
fig = plt.figure(figsize=(5,5))
ax = plt.axes(xlim=(-20, 20), ylim=(-20, 20))
Drunk = plt.Circle((0, 0), radius=1, fc='r')
def init():
Drunk.center = (0, 0)
ax.add_patch(Drunk)
return Drunk,
def animate(i):
x=walk[0]
y=walk[1]
Drunk.center = (x[i], y[i])
return Drunk,
anim = animation.FuncAnimation(fig, animate,
init_func=init,frames=361,interval=20,blit=True)
anim.save('drunk3.mp4')