Lesson 2-4 · Random Turns
Lesson 2-4 · Random Turns
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
importto 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.
What can `random.randint(1, 6)` return?
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.
Why does always turning exactly 180° cause a problem?
Activity · Make it random, then experiment
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
- 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:
Procedural worlds
Games generate endless different levels, loot, and maps from random numbers.
Robot vacuums
Simpler vacuums use semi-random turns to eventually cover a whole room — just like your bounce bot.
Simulations
Weather, traffic, and epidemic models run millions of random trials to predict outcomes.
Wrap-up
- What does
importdo? (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.)