You can download this code by clicking the button below.
This code is now available for download.
This function simulates a simple random walk process, given the initial position and number of steps, it returns a list of positions after each step.
Technology Stack : random
Code Type : Function
Code Difficulty : Intermediate
def random_walk(x, y, steps):
import random
directions = ['up', 'down', 'left', 'right']
walk = [(0, 0)]
for _ in range(steps):
direction = random.choice(directions)
if direction == 'up':
y += 1
elif direction == 'down':
y -= 1
elif direction == 'left':
x -= 1
elif direction == 'right':
x += 1
walk.append((x, y))
return walk