You can download this code by clicking the button below.
This code is now available for download.
Generate a random walk path, moving one unit at a time, with directions being up, down, left, or right.
Technology Stack : random (random numbers)
Code Type : Function
Code Difficulty : Beginner
def random_walk(num_steps):
import random
x, y = 0, 0
for _ in range(num_steps):
direction = random.choice(['up', 'down', 'left', 'right'])
if direction == 'up':
y += 1
elif direction == 'down':
y -= 1
elif direction == 'left':
x -= 1
elif direction == 'right':
x += 1
return x, y