Skip to main content

Lesson 2-4 · Random Turns

Module 2 · Line Tracking

Lesson 2-4 · Random Turns

50–60 minimport & randomBreaking the patternSlides (PowerPoint)
👩‍🏫

Teacher mode is on. Toggle it off (bottom-right) to preview the student view.

Your bounce robot has a flaw: turning exactly 180° every time, it just retraces the same path back and forth and never explores the rest of the circle. The fix is randomness — turning a different amount each bounce. To get it, you'll import one of Python's built-in toolboxes.

Learning Objectives

By the end of this lesson you will be able to:

  • Explain what a module is and use import to bring one in
  • Generate random integers with random.randint(a, b)
  • Say why always turning 180° traps the robot in a pattern
  • Write a random-angle version of your bounce test, from an outline

Modules and import​

Python ships with thousands of ready-made tools grouped into modules — think of each as a toolbox. To use one, you import it at the top of your file. You've done this already for the robot:

from XRPLib.reflectance import Reflectance

For random numbers you import Python's built-in random:

import random

for i in range(5):
number = random.randint(1, 10)
print("Random number:", number)

random.randint(1, 10) hands back one random whole number from 1 to 10, including both ends. Run it again and you'll (usually) get a different number.

Knowledge Check

What can `random.randint(1, 6)` return?

Knowledge Check

Where do import statements belong?

Random bounce driving​

The only change from Lesson 2-3 is in the if branch: instead of a fixed turn(180), pick a random angle first and turn by that. Nothing else in the program moves. In outline:

import random # at the top of robot.py, with the other imports

def test_random_bounce():
# a copy of test_bounce(), with the if branch becoming:
# stop
# pick a random whole number of degrees, 90 to 270
# print it, so you can see each choice
# turn by that many degrees
pass

Paste the new test above RUN and keep test_bounce() as it is — by the end of the module your file is a record of everything the robot has learned to do, and the fixed-angle bounce is part of that.

Each bounce now picks a fresh angle between 90° and 270°, so the robot fans out and covers the whole circle instead of pacing the same line. Why that range and not 0 to 360? Because the robot has to meaningfully change direction: an angle near 0 barely turns it, and it drifts out of the circle; an angle near 180 sends it straight back the way it came. The band in the middle is where exploring happens.

Knowledge Check

Why does always turning exactly 180° cause a problem?

Activity · Make it random, then experiment​

An animation, seen from above. The robot drives forward from the middle of a taped circle until its sensor reads the tape. Each time it stops, picks a random angle between 90 and 270 degrees, prints it and turns by that much before driving on. A blue line traces its path, and the lines fan out across the whole circle instead of retracing one line. Beside it, the outline of test_random_bounce() highlights the line that is running and shows the angle each bounce picked.
What step 1 should look like with randint(90, 270). Your angles will be different every run, which is the point.

Step 1 · The random turn​

Add import random at the top of robot.py, copy the body of test_bounce() into test_random_bounce(), and change the if branch as outlined. Set the run line to test_random_bounce() and run it: the printed angle should be different on (almost) every bounce, and the robot should wander the whole circle instead of pacing one line.

Step 2 · Try three ranges​

Try randint(170, 190), randint(90, 270) and randint(45, 315), and write down, for each, how the robot's coverage of the circle changes. Which one explores best, and why?

Step 3 · Challenge — random speed too​

randint() only gives whole numbers, and set_effort() wants a value between 0 and

  1. Pick a whole number in a range you choose, divide it to get an effort between 0.2 and 0.5, and drive at that speed until the next bounce.

Real-world connections​

Randomness is a real engineering tool, not just chaos:

Games

Procedural worlds

Games generate endless different levels, loot, and maps from random numbers.

Cleaning

Robot vacuums

Simpler vacuums use semi-random turns to eventually cover a whole room — just like your bounce bot.

Science

Simulations

Weather, traffic, and epidemic models run millions of random trials to predict outcomes.

Wrap-up​

  • What does import do? (Brings in a module's tools so you can use them.)
  • What does random.randint(1, 6) return? (One whole number, 1–6 inclusive.)
  • Why is a random turn better than a fixed 180° here? (It stops the robot retracing the same path.)

Resources​