Simulating a Random Walk
1

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

- First Enter your name:
- Then
  .
- All answers that you subsequently enter/edit will be auto-saved to this sheet.

Option 2: Continue, or view, previous work

- All answers that you subsequently enter/edit will be auto-saved to this sheet.
- The first time you re-load an answers sheet - within a single session on Elan - you will be asked to confirm that you understand this.

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

Step Completed 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

Step Completed 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

Step Completed 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:

- Delete the second `call` to circle - as the random walk simulation needs only one.
- Insert, immediately after the definition of `t` within `main` the instruction: `call circle(t, 50)`

and run the program again.

Does hiding the Turtle affect what it draws?

Hint 4-1: the complete code

#### Notes

Step Completed Total hints used: /

# Step 5: draw a random walk

#### Notes

Step Completed 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

Step Completed 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

Step Completed Total hints used: /

# Congratulations! You have completed this worksheet

If you have time, try any of the following:

- Simulate a larger number of walks e.g. 100. This will give a more accurate figure for the average number of paces.
- Make the pace smaller but keep the circle's radius the same size as now. The average number of paces to reach the edge will be larger. But does it increase in proportion? Try three different values and plot the average against pace size.
- Go back to the original values, but make a small change to the range of possible turn-angles between paces: -60 to + 60 degrees, or -120 to + 120, say. How does this change the average number of paces? Is the relationship linear?
- Read more about the nature and application of Random Walks on Wikipedia.