Teacher's Guide
Chapter 8 / Level 6: Random Module
Teaching Objective:
- Student understands what the random module offers, and it’s unique uses
- Learns about simulations and the practical applications of using random numbers
- Learns the syntax and usage of the various random module functions
Guide
In this exercise we’ll be working with the random module, which is activated by import random . This allows you to generate random values or pick random values from certain sources. This is useful for simulating errors and calculating potential data, which is what we’re going to be using it for in this level. The functions we’ll be using from the improt module are as follows:
random.seed(): Sets the seed for random generation, takes one argument being the seed number you’d like to use. This means any random function used with any given seed will return the same results when used. This is used to exert some control over random generation and be able to recreate certain results.random.random(): Returns a random float point between0and1.random.randint(): Takes two(2) arguments, generates a number between the two arguments, including the numbers themselves. The result is returned in whole numbers/integer.random.uniform(): Same as the previous function but returns a random float point number instead of an integer.random.triangular(): Same as the previous function but has an extra argument as a modifier called mode. The mode needs to be a number between the two numbers in the random range. The random number generated will be a number statistically closer to the mode rather than a purely random number between the first two(2) arguments.random.choice(): Takes list as an argument, picks one (1) item from the list at random.random.choices(): Generates a random list sampled from another list, takes three(3) arguments: a list you would like to sample from, another list you can outline informing how much weight you would like to sample from each item in the list you’re sampling from and finally a variable named k you can assign a value to which outlines how many samples you would like to take from the list. The number of samples can be longer than the list itself as it will generate duplicates based on the weights you assigned.random.shuffle(): Takes a list as an argument and shuffles it randomly.random.sample(): Takes two arguments, a list to sample from and the number of samples you would like to take. A simplified version ofrandom.choices().
Start off by walking to the light X mark and using the read() function to acquire data to be used with functions and revealing the value of constants. Use the data to set random.seed() to add an element of control to the random generation.
import random
.................
await player.read()
random.seed(10)
Walk to the dark X mark over the blue carpet, create three variables: selection , delay and defect. Use the list constant sectors with random.choice() to populate selection . Use delay to store the value of random.random() and defect to store the value of random.randint() . Use the arguments 0 and 15 with random.randint() to generate a percentage between those two numbers. There variables and functions are used to extract samples and generate possible margins for errors so they can be studied.
selection = random.choice(sectors)
delay = random.random()
defect = random.randint(0,15)
Use the selection , delay and defect variables with the pre-written write() function to chart down the data.
Walk to gold X mark and use the read() function to gather data points to run a simulation. Walk to dark X mark over red carpet and populate a list named weights with the data points on each material displayed in the previously read memo. Create a list named simulation and store the value of random.choices() . Use the materials list constant, weights and assign the value of k to the number of units outlined in the previously read memo.
await player.read()
................
weights = [400, 600 , -insert value-, -insert value-]
simulation = random.choices(materials, weights , k = insert sampling units )
Use the simulation list with the pre-written write() function.
Walk to dark X marl over green carpet and use random.shuffle() with the simulation list to shuffle it. Create a new list named sample and store the value of random.sample(), with the simulation list and the variable k set to 3 as arguments to retrieve three (3) samples.
random.shuffle(simulation)
sample = random.sample(simulation, k=3)
Use simulation and sample lists with the pre-written write() function.
Walk to dark X mark over purple carpet, create a variable named defect and store the value of random.uniform() with arguments 3 and 10 representing the sample sizes, to generate potential material defects. Create variable named losses and store the value of random.triangular() with the arguments 200 , 600 and 400 . This reperents the material weights from before and generating potential losses.
defect = random.uniform(3, 10 )
losses = random.triangular(200, 600, 400)
Use the defect and losses variables with the pre-written write() function to complete the level.
Map overview and path

Code Solution
import player
import random
player.move_forward(4)
player.turn_left()
player.move_forward(2)
player.turn_left()
await player.read()
player.turn_right()
player.move_forward(2)
player.turn_left()
random.seed(10)
selection = random.choice(sectors)
delay = random.random()
defect = random.randint(0,15)
await player.write(
"Simulation sample for equipment failure-\nSelection: %s\nMargin: %s%%\nDefect rate: %s"
% (selection,delay,defect))
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward(3)
player.turn_right()
await player.read()
player.turn_left()
player.move_forward()
player.turn_right()
weights = [400, 600 , 300, 200]
simulation = random.choices(materials, weights , k = 10)
await player.write(
"Run Simulation-\n%s"
% (simulation))
player.turn_left()
player.move_forward(2)
player.turn_right()
player.move_forward(2)
player.turn_right()
player.move_forward()
player.turn_left()
random.shuffle(simulation)
sample = random.sample(simulation, k=3)
await player.write(
"New Arrangement: %s\nSampling: %s"
% (simulation,sample))
player.turn_right()
player.move_forward(4)
player.turn_left()
defect = random.uniform(3, 10 )
losses = random.triangular(200, 600, 400)
await player.write(
"Simulating potential errors-\nDefective materials: %s\nPotential losses: %s%%"
% (defect,losses))