Random walks with Python Turtle Graphics

In this post we will be looking at an idea from mathematics called a random walk. In a random walk, each step in a process is determined randomly and we are interested in the state of the process after a given number of steps. There are examples of this phenomenon happening all around us. For example the following process can all be modeled as random walks:

  • The movement of molecules in liquids and gasses
  • Changes in share prices
  • Sports results for a given period, such as a football season
  • Biological evolution via gene variation
  • Estimating the size of the World Wide Web

One great way to get a feel for random walks is the following simulation.

A walker begins a a particular location in a very regularly laid out city (like many in the United States), where each block is the same size and the roads intersect at right angles.

At every intersection the the walker reaches, they can either continue in the same direction or choose one of the other directions from North, South East or West.

We can decide how many block the walker will travel, but the directions at each intersection are determined randomly (maybe the walker throws a 4-sided dice…)

How far from the starting point will the walker end up?

Obviously since this is a random process, we can not answer this exactly, but we can get a sense of the range of possible results and their likelihood. In a future article we will look at this phenomena more deeply. We will write a function to calculate the distance from the start, and then use the idea of a Monte Carlo Simulation to perform a large number of trials to see if there are any patterns we can detect.

For now, I’ve provided the code for you to run the random walk simulation. You will need to run it on a “proper” version of python installed on your machine as opposed to one of the browser-based implementation such as https://trinket.io/, as it uses some fairly advanced features of the Python Turtle Module.

Code listing for Python Random Walk Simulator

A few points about the code:

  • You will have to choose values for the constants carefully if you change them, as only some values place the walker in a street!
  • Controlling the size of the turtle with turtle.shapesize() requires some unexpected maths as demonstrated in builder.shapesize(BLOCK_SIZE / STAMP_SIZE)
  • The random_walk() function is implemented very succinctly. There are ways which are probably clearer. If you decide to make a more “obvious” version using if...elif.. etc., don’t forget to make sure that failed attempts (ones which would place to walker off-grid) are not counted in the total number of steps taken.

I hope you found this interesting. Don’t forget to subscribe if you want to be on our mailing list for news and offers relating to Python programming and GCSE/A Level Computer Science.

Leave a Repl​​​​​y

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Get the code for the classic Retro Alien Blitz Game using Python Turtle Graphics when you join our mailing list