Simulating a Random Walk

First, set the browser to Full Screen view, to give this worksheet and your code as much space as possible.

Then choose either one of the two options below:

Option 1: Start this worksheet from scratch

Option 2: Continue, or view, previous work

This worksheet is copyright © Richard Pawson 2025, and protected by the Creative Commons license: Attribution-NonCommercial-NoDerivatives 4.0 International. You may freely make and distribute copies of this worksheet as is, but if you 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.

Step 1: Introduction

Notes

Total hints used: /

Step 2: Using Turtle graphics

Copy the code shown at the end of the video into the code editor. Then modify such that it will draw an octagon with a side of length 20.

Make sure your code is working correctly before completing the step. Use the hints available only if you need to.

Hint 2-1: changes needed
Hint 2-2: the complete code

Notes

Total hints used: /

Step 3: Draw a circle, centred on the display

(There is no video for this step.)

Change the code again so that it draws a circle that fits within the display and is centred within in.

Make sure your code is working correctly before completing the step. Use the hints available only if you need to.

Hint 3-1: changes needed
Hint 3-2: the complete code

Notes

Total hints used: /

Step 4: create a circle procedure

Get your code to the same state as that shown at the end of the video (if necessary replaying the video and pausing), and test that it draws two concentric circles of different radius. Then:

and run the program again.

Does hiding the Turtle affect what it draws?

Hint 4-1: the complete code

Notes

Total hints used: /

Step 5: draw a random walk

Notes

Total hints used: /

Step 6: count the paces

Get your code to the same state as that shown at the end of the video (if necessary replaying the video and pausing), and test that it works correctly.

Run the program 10 times, and record here the smallest, and the largest, number of paces taken to reach the edge of the circle.

Notes

Total hints used: /

Step 7: simulate multiple walks

Get your code to the same state as that shown at the end of the video (if necessary replaying the video and pausing), and test that it works correctly.

What value did you get for the average number of paces?

Try increasing the number of walks from 20 to 100, noting that you will have to change 20 to 100 in two separate places.

What value did you get for the average number of paces after 100 walks?

The fact that we had to change the 20 to 100 in two locations is an example of breaking what principle of good programming?

What would be a simple way to improve the code so that you could change the number of walks required in just one place?

Notes

Total hints used: /

Congratulations! You have completed this worksheet

If you have time, try any of the following: