Random Library
Python's random module generates pseudo-random
numbers — numbers that look random but are produced by a deterministic algorithm called
a seed.
Learning Objectives
- 12.6.1.1 Use the functions of the library Random to get a pseudo-random number
- 12.6.1.3 Identify code snippets that use random sequence generation
Conceptual Anchor
The Loaded Dice Analogy
A pseudo-random number is like rolling dice — the result looks random, but if
you knew the exact position, spin, and force, you could predict it. The seed is
that starting position. Same seed → same "random" sequence every time.
Rules & Theory
Key random Functions
| Function | Description | Example |
|---|---|---|
random.random() |
Float in [0.0, 1.0) | 0.37444887175646646 |
random.randint(a, b) |
Integer in [a, b] (inclusive) | random.randint(1, 6) → 4 |
random.randrange(start, stop, step) |
Random from range (exclusive stop) | random.randrange(0, 100, 5) → 35 |
random.uniform(a, b) |
Float in [a, b] | random.uniform(1.5, 9.5) → 6.28 |
random.choice(seq) |
Random element from sequence | random.choice(["a","b","c"]) → "b" |
random.shuffle(list) |
Shuffle list in-place | random.shuffle(cards) |
random.sample(seq, k) |
k unique elements from sequence | random.sample(range(100), 5) |
random.seed(n) |
Set seed for reproducible results | random.seed(42) |
Import Methods
import random # Use as: random.randint(1, 6)
from random import randint # Use as: randint(1, 6)
from random import * # All functions without prefix (not recommended)Worked Examples
1 Simulating a Dice Roll
import random
# Roll a standard 6-sided die
roll = random.randint(1, 6)
print(f"You rolled: {roll}")
# Roll two dice
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
total = dice1 + dice2
print(f"Dice: {dice1} + {dice2} = {total}")2 Generating a Random Password
import random
import string
# All possible characters
chars = string.ascii_letters + string.digits + "!@#$%"
# Generate 12-character password
password = ''.join(random.choice(chars) for _ in range(12))
print(f"Password: {password}")
# Example output: Password: kR7$mPx2Qn!v3 Reproducible Results with seed()
import random
random.seed(42)
print(random.randint(1, 100)) # Always: 82
print(random.randint(1, 100)) # Always: 15
print(random.randint(1, 100)) # Always: 4
# Reset seed → same sequence!
random.seed(42)
print(random.randint(1, 100)) # Again: 824 Identifying Random Code Snippets
# Which snippets generate random sequences?
# ✓ YES — generates random sequence of 5 numbers
nums = [random.randint(1, 50) for _ in range(5)]
# ✓ YES — randomly shuffles a list
deck = list(range(1, 53))
random.shuffle(deck)
# ✗ NO — deterministic, not random
nums = [i * 2 for i in range(5)] # [0, 2, 4, 6, 8]
# ✓ YES — samples 3 unique random items
winners = random.sample(["Alice","Bob","Clara","Dan"], 3)Common Pitfalls
randint vs randrange
randint(1, 10) includes BOTH 1 and 10. randrange(1, 10) includes 1 but
NOT 10 (just like range()). This is the most common source of off-by-one errors.
shuffle() Returns None
random.shuffle() modifies the list in-place and returns
None. Don't write x = random.shuffle(mylist) — x will be
None!
Tasks
List 5 functions from the random module and explain what each returns.
Write a program that simulates flipping a coin 100 times and counts heads/tails.
Write a number guessing game: the computer picks a random number 1–100, the user guesses, and gets "higher/lower" hints.
Given a code snippet, identify which lines use random generation and explain the possible outputs.
Self-Check Quiz
Q1: What does random.randint(1, 6) return?
Q2: What is a pseudo-random number?
Q3: What is the difference between choice() and
sample()?
choice() picks ONE
random element. sample(seq, k) picks k UNIQUE elements without replacement.