← VCE Software Development U4O1

Random numbers with import random

Gives your program the random module. Then random.randint(1, 6) gives a random whole number from 1 to 6, and random.choice(my_list) picks a random item from a list.

Understanding Random numbers with import random in VCE Software Development U4O1

What it does: random.randint(1, 6) makes the number and the = stores it in the variable roll. random.choice() takes a list and returns one random item from it. Python uses the import keyword to load a module. The line import random gives your program access to the random module. randint needs two numbers: the lowest and the highest value it can give. random.randint(1, 6) can return 1, 2, 3, 4, 5 or 6. The smaller number goes first and the larger number goes second. randint(15, 5) causes an error. randint(a, b) picks a whole number between a and b, and both ends can be chosen, like rolling a dice.

Common mistakes: A single = assigns a value. To compare dice with 6 you need ==, so the line should be: if dice == 6:. The module name is spelled wrong. It must be random, so the line should be: import random.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 115–124) and word search (Modules 115–171).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line must come at the top of your program before you can use random.randint()?
include randomimport random
Matches
num = random.randint(1, 10) (no import random line at the top)
↓
The import random line is missing, so Python does not know what random is and shows a NameError. Add import random at the top.
Applying
Meanings
Term
import random
Meaning
Gives your program the random module. Then random.randint(1, 6) gives a random whole number from 1 to 6, and random.choice(my_list) picks a random item from a list.
Practice
import random
Testing
Gaps
random
Check Answer
Spots
3print('You rolled', dice)
4if dice = 6:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Stag' badge — Forest Legend, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line must come at the top of your program before you can use random.randint()?
A. include random
B. import random ✓
Python uses the import keyword to load a module. The line import random gives your program access to the random module.
2. Which code gives a random whole number from 1 to 6, like rolling a dice?
A. random.randint(1, 6) ✓
B. random.randint(6)
randint needs two numbers: the lowest and the highest value it can give. random.randint(1, 6) can return 1, 2, 3, 4, 5 or 6.
3. True or false: random.randint(1, 10) can return the number 10.
A. False
B. True ✓
Both ends are included in randint. So random.randint(1, 10) can give 1, 10, or any whole number in between.
4. Which line correctly stores a random number in a variable?
A. num = random.randint(1, 100) ✓
B. num = randint(1, 100)
After import random, you must write random. before randint. Without it, Python does not know what randint is.
5. Which code picks a random item from the list pets = ["cat", "dog", "fish"]?
A. random.choice(pets) ✓
B. random.randint(pets)
random.choice() picks one random item from a list. randint is for whole numbers and needs two numbers, not a list.

Match the Pairs

import random → Gives your program the random module so you can use random.randint() and random.choice()
random.randint(1, 6) → Gives a random whole number from 1 to 6, including both 1 and 6
Roll a dice and save the random whole number from 1 to 6 in a variable → roll = random.randint(1, 6)
Pick a random item from a list of snacks → random.choice(snacks)
num = random.randint(1, 10) (no import random line at the top) → The import random line is missing, so Python does not know what random is and shows a NameError. Add import random at the top.
import random num = randint(1, 10) → randint is called without random. in front. It should be random.randint(1, 10).

Key Term

import random
Gives your program the random module. Then random.randint(1, 6) gives a random whole number from 1 to 6, and random.choice(my_list) picks a random item from a list.

Fill in the Blanks

1. import random
Hint: Type the keyword that loads a module into your program.
2. import random
Hint: Type the name of the module that gives you random numbers.
3. roll = random.randint(1, 6)
Hint: Type the function name that gives a random whole number between two values.
4. roll = random.randint(1, 6)
Hint: Type the highest number the dice can roll.
5. pick = random.choice(colours)
Hint: Type the function name that picks a random item from a list.

Spot the Error

Question 1
A import random
B dice = random.randint(1, 6)
C print('You rolled', dice)
D if dice = 6:
E print('Bonus roll!')
Line D — A single = assigns a value. To compare dice with 6 you need ==, so the line should be: if dice == 6:
Question 2
A import randon
B num = random.randint(1, 10)
C print(num)
Line A — The module name is spelled wrong. It must be random, so the line should be: import random
Question 3
A import random
B n = randint(1, 100)
C print('Number:', n)
Line B — With just import random, randint must be called with the module name in front. It should be: n = random.randint(1, 100)
Question 4
A import random
B pets = ['cat', 'dog', 'fish']
C pick = random.choose(pets)
D print('Your pet:', pick)
Line C — The function that picks a random item from a list is random.choice(), not random.choose(). It should be: pick = random.choice(pets)
Question 5
A import random
B colours = ['red', 'blue']
C c = random.choice[colours]
D print(c)
Line C — Functions are called with round brackets (), not square brackets []. It should be: c = random.choice(colours)

Student with a class code? Join your class →