Random Walk visualized with Python

Using the simple graphics library turtle to create a random walk

Introduction

A random walk in mathematics is a random process that describes a path that consists of a succession of random steps on some mathematical space (from Wikipedia).

What we will code

A simple graphics library, turtle, is part of the core Python stack and can be used to draw graphics to the screen. We shall use it to draw the path of our random walk in a two dimensional space

Importing libraries

Simply import the random and the turtle module. No need to install anything any external libraries :)

import random
import turtle

Set up the screen and the turtle object

These are some setup steps before we begin walking

win = turtle.Screen()
win.colormode(255)        # To make the colors appear properly
walker = turtle.Turtle()  # Our turtle object
walker.pensize(5)         # Set the width of the pen stroke
walker.speed(3)
walker.hideturtle()       # Make the turtle invisible, only the path will be visible

Write the algorithm for random walk

Define initial values of x position and y position of the turtle

x = y = 0

(0, 0) is the middle of the screen. y increases upwards and decreases downwards, while x increases to the right and decreases to the left. We shall use this information to choose how to move in a particular direction.

Inside an infinite loop, choose a random color to draw the line and a random direction to go in

while True:
  # Choose random r, g and b values. r, g, b values are used to define colors
  # randint(0, 255) chooses a random integer between 0 and 255
  r = random.randint(0, 255)
  g = random.randint(0, 255)
  b = random.randint(0, 255)

  step = random.choice(['n', 's', 'e', 'w']) # Choose a random direction, North, South, East, or West
  if step == 'n':
    y += 1
  elif step == 's':
    y -= 1
  elif step == 'e':
    x += 1
  else:
    x -= 1

  # Set the random color
  walker.color((r, g, b))
  # Go to the random point
  walker.goto(x*20, y*20)    # Multiplying by 20 to scale the magnitude of movement

Final step

Put the entire thing inside a function

def random_walk():
    x = y = 0
    while True:
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)

        step = random.choice(['n', 's', 'e', 'w']) # Choosing direction randomly
        if step == 'n':
            y += 1
        elif step == 's':
            y -= 1
        elif step == 'e':
            x += 1
        else:
            x -= 1

        walker.color((r, g, b))
        walker.goto(x*20, y*20)

Finally call the function and run the main loop

random_walk()
turtle.mainloop()

On running the program you will see the turtle moving in a random manner, with each line being drawn in a random color.

Conclusion

You can play with the pensize, speed of the turtle and the scaling number (here I used 20) to modify the behavior of the program. You can find the full code here on GitHub. Learn more about turtle here. Happy coding.