You can download this code by clicking the button below.
This code is now available for download.
The function simulates a random walk process. Given the number of steps and the starting point, the function returns the coordinates of each step.
Technology Stack : The function uses the following packages and technologies: Python standard library (random module)
Code Type : The type of code
Code Difficulty : Advanced
def random_walk(num_steps, start_point=(0, 0)):
"""
Simulate a random walk where the walker moves in random directions.
Args:
num_steps (int): The number of steps the walker will take.
start_point (tuple): The starting point of the walker.
Returns:
list: A list of tuples representing the coordinates of each step.
"""
import random
x, y = start_point
path = [(x, y)]
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
path.append((x, y))
return path