Class 12 Python – Random Module (4 Pages Notes)
1. Introduction to Random Module
Python’s random module is used to generate random numbers and perform random
operations.
It is widely used in simulations, games, data sampling, and testing programs.
To use it, you must import the module:
import random
2. Functions in Random Module
A. Basic Random Number Generators
1. random()
o Returns a random float between 0.0 and 1.0.
2. print(random.random()) # e.g., 0.3748
3. uniform(a, b)
o Returns a random float in the range [a, b].
4. print(random.uniform(1, 10)) # e.g., 7.43
5. randint(a, b)
o Returns a random integer between a and b (inclusive).
6. print(random.randint(1, 6)) # like a dice roll
7. randrange(start, stop, step)
o Returns a random number from a range.
8. print(random.randrange(1, 10, 2)) # odd numbers between 1 and 9
B. Random Choices from Sequences
1. choice(sequence)
o Returns a random element from a list, tuple, or string.
2. colors = ["red", "blue", "green"]
3. print(random.choice(colors))
4. choices(population, k=n)
o Returns a list of n random elements (with replacement).
5. print(random.choices([1, 2, 3, 4], k=3)) # e.g., [2, 4, 2]
6. sample(population, k=n)
o Returns n unique random elements (without replacement).
7. print(random.sample([1, 2, 3, 4, 5], 3)) # e.g., [5, 1, 3]
C. Shuffling Data
shuffle(list)
o Randomly reorders elements of a list.
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers) # e.g., [3, 1, 5, 4, 2]
D. Seeding Random Numbers
seed(n)
o Sets the starting point of the random generator (useful for reproducibility).
random.seed(10)
print(random.randint(1, 100))
If the same seed is used, the same random numbers are generated each time.
3. Applications of Random Module
Simulations: Rolling dice, lottery, scientific experiments.
Games: Card shuffling, random moves in AI.
Data Science: Random sampling, model testing.
Security: Generating OTPs and passwords.
Example – Dice Roll Simulation
import random
print("You rolled:", random.randint(1, 6))
Example – Password Generator
import random, string
chars = string.ascii_letters + string.digits + "!@#$%"
password = "".join(random.sample(chars, 8))
print("Generated Password:", password)
4. Differences Between Functions
Function Output Type Example Usage
random() Float [0,1) Probability simulation
uniform(a,b) Float Random float in range
randint(a,b) Integer Dice roll, guessing game
randrange() Integer Random step values
choice() Single item Pick a card, color, etc.
choices() List Multiple picks (with repeat)
sample() List Multiple picks (no repeat)
shuffle() None Shuffling a deck of cards
seed(n) None Reproducibility in testing
5. Practice Questions
1. Write a program to simulate rolling a dice 10 times.
2. Generate 5 random floating-point numbers between 10 and 50.
3. Pick 3 random students from a list using sample().
4. Write a program to shuffle a deck of 52 playing cards.
5. Generate a random OTP of 6 digits.
6. Demonstrate the effect of seed() by generating the same random sequence twice.
6. Exam Tips
Be clear about difference between randint() and randrange().
sample() vs choices() is a frequently asked board exam question.
Always remember random() → float between 0 and 1.
Be ready to write simple programs using random functions.