-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_walk_2D.py
35 lines (30 loc) · 1.15 KB
/
random_walk_2D.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
# Example on page 73 of the textbook.
import random
import numpy as np
import matplotlib.pyplot as plt
N = 1000; # number of steps.
d = 1; # step length (e.g., in meter.)
x = np.zeros(N + 1); # x coordinates e.g., x[0, 0 , 0, ..., x1001]
y = np.zeros(N + 1); # y coordinates
x[0] = 0;
y[0] = 0; # set initial position
for i in range(0, N, 1):
r = random.random(); # random number in [0,1)
if 0 <= r < 0.25: # move north
y[1 + i] = y[i] + d; # Starting from y[1] since y[0] has been set to 0.
x[1 + i] = x[i];
elif 0.25 <= r < 0.5: # move east
x[1 + i] = x[i] + d; # X-axis is west-east bound.
y[1 + i] = y[i];
elif 0.5 <= r < 0.75: # move south
y[1 + i] = y[i] - d; # Y-axis is north-south bound.
x[1 + i] = x[i];
else: # move west
x[1 + i] = x[i] - d;
y[1 + i] = y[i];
# plot path (mark start and stop with blue o and *, respectively)
plt.plot(x, y, 'r--', x[0], y[0], 'bo', x[-1], y[-1], 'b*'); # [1]
plt.xlabel("x"); plt.ylabel("y");
plt.show();
# References:
# 1. Indexing an Array from the End. (Page 50 on the textbook.)