Python generates random numbers using the built-in random module. Import it once at the top of your program, then call randint for a whole number in a range, choice to pick an item from a list, or shuffle to reorder a list. Randomness is what makes games, quizzes and simulations feel alive.
Step 1 — Import the module
Nothing random works until you import it:
import random
Put this on the first line of your program. Forgetting it produces NameError: name 'random' is not defined, which is the single most common error in this topic.
Step 2 — Generate a whole number with randint
import random
number = random.randint(1, 6)
print(number)
randint(1, 6) returns a whole number from 1 to 6, and both ends are included — so 1 and 6 are both possible results. That makes it perfect for dice.
Run the program repeatedly and you will get different answers. Nothing else in your program changes; the value comes fresh from the module each time.
A pair of dice:
import random
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
total = die1 + die2
print("You rolled", die1, "and", die2, "- total", total)
Note that randint is called twice. Calling it once and using the result for both dice would give you two identical values every single roll — an easy bug to write and a surprisingly hard one to spot.
Step 3 — Use randrange when you want to skip values
randrange works like range: the lower bound is included, the upper bound is not.
random.randrange(1, 7) # 1 to 6 — same as randint(1, 6)
random.randrange(0, 101, 10) # 0, 10, 20 ... 100
The third argument is a step, so the second example only ever produces multiples of ten. This is useful for generating prices, scores in fixed increments, or coordinates on a grid.
Step 4 — Pick an item with choice
For anything that is not a number, use choice:
import random
colours = ["red", "green", "blue", "yellow"]
picked = random.choice(colours)
print("The chosen colour is", picked)
choice works on any list, including a list of questions, names or images. It is the natural tool for a quiz that asks questions in a random order, or a game that picks a random opponent.
For more than one item without repeats, use sample:
team = random.sample(colours, 2) # two different colours
Step 5 — Reorder a list with shuffle
import random
cards = ["A", "K", "Q", "J", "10"]
random.shuffle(cards)
print(cards)
shuffle changes the list in place — it rearranges the original rather than returning a new one. This catches people out:
cards = random.shuffle(cards) # WRONG - cards is now None
shuffle returns nothing, so assigning its result throws away your list. Call it on its own line.
A worked mini project: a times-table quiz
import random
score = 0
for question in range(5):
a = random.randint(2, 12)
b = random.randint(2, 12)
answer = int(input(str(a) + " x " + str(b) + " = "))
if answer == a * b:
print("Correct!")
score = score + 1
else:
print("Not quite - the answer was", a * b)
print("You scored", score, "out of 5")
Two fresh random numbers are generated inside the loop, so every question is different. Generating them before the loop would ask the same question five times — another version of the two-dice mistake, and worth checking for whenever a program feels less random than expected.
Are these numbers really random?
Not strictly. random produces pseudo-random numbers: they come from a mathematical formula that starts from a value called the seed and generates a sequence which looks random but is completely determined by that seed.
You can see this by setting the seed yourself:
import random
random.seed(42)
print(random.randint(1, 100))
print(random.randint(1, 100))
Run that program as many times as you like and it prints the same two numbers every time. Without a call to seed, Python picks a seed from the system — usually derived from the current time — so each run differs.
This behaviour is genuinely useful. When testing a game, a fixed seed makes the results repeatable so you can reproduce a bug exactly. For anything security-related, though, predictability is a fatal flaw, which is why cryptographic keys are not generated with this module.
Common mistakes to avoid
- Forgetting
import random. - Calling
randintonce and reusing the value where you needed several different values. - Assigning the result of
shuffle, which sets your variable toNone. - Expecting
randrange(1, 6)to include 6. It does not —randint(1, 6)does. - Generating random values outside a loop when each iteration should be different.
- Forgetting
int()aroundinput()when comparing an answer to a number.
Frequently asked questions
What is the difference between randint and randrange?
randint(a, b) includes both endpoints, so randint(1, 6) can return 1, 2, 3, 4, 5 or 6. randrange(a, b) excludes the upper endpoint, matching the behaviour of range, so randrange(1, 6) returns 1 to 5. randrange also accepts a step value, letting you generate only multiples of a number. Use randint for dice and similar inclusive ranges, and randrange when you want range-style behaviour or a step.
Why does my program produce the same "random" number every time?
Almost always because the value is generated once and then reused. If random.randint(1, 6) is called before a loop and the result is used inside it, every iteration sees the same value. Move the call inside the loop. The other possibility is that random.seed() has been called with a fixed number, which deliberately makes the sequence repeat identically on every run.
Can I generate a random decimal rather than a whole number?
Yes. random.random() returns a decimal between 0.0 and 1.0, and random.uniform(a, b) returns a decimal between the two values you give it — random.uniform(1, 10) might return 4.7382. Use round() if you want fewer decimal places. For most KS3 projects whole numbers are what you want, but decimals are useful for simulations and for positioning things smoothly on screen.
Is the random module safe to use for passwords?
No. The random module is designed to be fast and statistically well-behaved, not unpredictable to an attacker — given enough output, its sequence can be worked out. Python provides a separate secrets module for anything security-sensitive such as passwords, tokens or keys. For games, quizzes, simulations and shuffling, random is exactly the right tool.
For Socratic KS3 computing tutoring — Python, projects and beyond — visit aitutors.me.