Random Walk

JIANWEI LI
Data Mining the City
1 min readNov 16, 2017
Random Walk
import randomdef setup():
global x,y
size (150,150)
x = width/2
y = height/2
def random_walk(n):
“Return coordinates after ’n’ block random walk.”
global x,y
print (x, y)
inc = 2for i in range(n):# random direction
step = random.choice([‘N’,’S’,’E’,’W’])
if step == ‘N’:
y = y+inc
elif step ==’S’:
y =y-inc
elif step ==’W’:
x=x-inc
else:
x=x+inc

return(x,y)
def draw():
i=0
line(random_walk(i)[0],random_walk(i)[1],random_walk(i+1)[0],random_walk(i+1)[1])
i+=1

--

--