Simulating a Random Walk
to continue. (After that any entries made into the worksheet will be automatically saved, and you can re-load the partially-completed worksheet in future - at which point you will be asked to auto-save it again).

Preliminaries

Total hints used: /

Iteration 1: Draw a shape with 'turtle graphics'

Purpose of this worksheet

Our objective is to investigate the mathematical idea of a 'random walk'. Imagine that you are standing in the centre of a circle with a radius of 10 paces. But instead of walking straight to the edge, with each pace you make a random turn - of up to to 90 degrees left or right. How many paces will it take to reach the edge? If the turning is truly random then each attempt will take a different number: the picture below shows three such paths taken:

Can we determine the average number of paces to reach the edge? Yes, if we could perform the experiment many times. And it turns out that being able to predict the average length of a different kinds of random walk has significant applications in many different fields of study: physics, biology, ecology, economics ..., even computer science. So we are going to build just such a simulation.

Total hints used: /

Iteration 2 - Draw the circle with a radius of 10 paces

Returning to our problem we defined the specific random walk to be simulated as getting from the centre to the edge of a circle with a radius of 10 paces. We now know how to draw a regular polygon and a circle may be described as a regular polygon with an infinite number of sides. But since the display's resolution is not infinite we can get away with less than that! 360 sides, turning 1 degree each time should suffice.

Total hints used: /

Iteration 3 - Draw a random walk

To simulate the random walk we need to repeatedly move the turtle one pace, and turn though a random angle of up to 90 degrees in either direction (to turn to the left, just make the angle negative). The method random() will generate a fractional number in the range 0 to 1, but there is also randomInt ('Int' being short for integer) that will generate a random whole number within a range specified by two integer arguments.

We could use another for for repetition, but the problem is that we don't know how many steps it will take to get from the centre to the edge. What we need is a loop that executes as many times as is needed - until something decides that it is time to stop. There are two such forms of loop: a while loop and a repeat loop, each one optimised toward slightly different circumstances, but for this particular challenge they would work equally well, so we'll just pick the first one.

Total hints used: /

Iteration 4 - simulate multiple walks

Our goal is to determine the average number of paces for a random walk to reach the edge of the circle. So the next step is to perform multiple walks.

Total hints used: /

Iteration 5 - Calculating the average number of paces

Total hints used: /

Well done - you've now completed the worksheet.

If you have time left, try any of the following:

This worksheet is copyright © Richard Pawson 2025, and protected by the Creative Commons license: Attribution-NonCommercial-NoDerivatives 4.0 International. If you copy and modify this worksheet you may not distribute your modified version (outside your own teaching institution) without the author's permission. Please email the author to report errors or suggest improvements.