Skip to main content

Lesson 4 · Random Turns

Module 2 · Line Tracking

Lesson 4 · Random Turns

50–60 minimport & randomBreaking the pattern
👩‍🏫

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
  • Modify your bounce program to turn a random angle each time

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 3 is swapping the fixed turn(180) for a random angle:

from XRPLib.reflectance import Reflectance
from XRPLib.differential_drive import DifferentialDrive
from XRPLib.board import Board
import random

reflectance = Reflectance.get_default_reflectance()
drivetrain = DifferentialDrive.get_default_differential_drive()
board = Board.get_default_board()

threshold = 0.5

board.wait_for_button()

while True:
left = reflectance.get_left()
right = reflectance.get_right()

if left > threshold or right > threshold:
drivetrain.stop()
angle = random.randint(90, 270) # a different turn each time
print("Line detected! Turning", angle, "degrees")
drivetrain.turn(angle)
else:
drivetrain.set_effort(0.3, 0.3)

Now each bounce picks a fresh angle between 90° and 270°, so the robot fans out and covers the whole circle instead of pacing the same line.

Knowledge Check

Why does always turning exactly 180° cause a problem?

Activity · Experiment

  1. Add import random and the random angle to your Lesson 3 program.
  2. Try three different ranges — 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?
  3. Challenge: randomize the speed too — speed = random.randint(20, 50) / 100 gives an effort from 0.2 to 0.5.

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