I wrote the code for a two dimensional random walk:
def r2walk(T):
x = np.zeros((T))
y = np.zeros((T))
x = [0]*T
y = [0]*T
for t in range(0,T):
walk = random.random()
if 0 < walk < .25:
x[t] = x[t-1] + 1
elif .25 < walk < .5:
x[t] = x[t-1] - 1
elif .5 < walk < 0.75:
y[t] = y[t-1] + 1
else:
y[t] = y[t-1] - 1
return x, y
I would like to be able to plot the path of the random walk on a x,y grid but am unsure how to proceed. Also I am very new to python and I would appreciate any tips on writing code more efficiently (or elegantly as your perspective might put it). Thank you in advance!