Course Progress44%
🍎 Python Basics Topic 44 / 100
⏳ 7 min read

The random Module

Generate random numbers, pick random items from lists, shuffle, and control reproducibility — everything you need to add chance to your programs.

"Randomness in programming is never truly random — but the random module makes it feel that way, and that is enough for games, simulations, and sampling."

— ShurAI

Random Numbers

python
import random

# Float between 0.0 and 1.0
print(random.random())            # 0.7342...

# Float between two values
print(random.uniform(1.0, 5.0))  # 3.218...

# Integer between a and b, INCLUSIVE on both ends
print(random.randint(1, 6))      # 1, 2, 3, 4, 5, or 6 — like a die

# Integer using range-style (stop is EXCLUSIVE)
print(random.randrange(0, 100, 5)) # 0, 5, 10, 15 ... 95

Picking from a List

python
fruits = ["apple", "mango", "banana", "guava", "pineapple"]

# Pick ONE random item
print(random.choice(fruits))        # e.g. "mango"

# Pick MULTIPLE — no duplicates (like drawing from a hat)
print(random.sample(fruits, 3))    # e.g. [’guava’, ’apple’, ’pineapple’]

# Pick MULTIPLE — duplicates allowed (like rolling a die twice)
print(random.choices(fruits, k=3))  # e.g. [’mango’, ’mango’, ’banana’]
choice()
Pick one item.
Returns a single value.
sample(lst, k)
Pick k unique items.
No duplicates.
choices(lst, k=k)
Pick k items.
Duplicates allowed.

Shuffling a List

python
deck = ["A", "2", "3", "4", "5", "J", "Q", "K"]
print("Before:", deck)

random.shuffle(deck)   # shuffles the list IN PLACE, returns None
print("After :", deck)
# After: [’Q’, ’2’, ’A’, ’K’, ’5’, ’3’, ’J’, ’4’]  (varies)

Seeding — Reproducible Results

python
# Same seed → same sequence every run — great for testing
random.seed(42)
print(random.randint(1, 100))   # always the same value
print(random.randint(1, 100))   # always the same next value

# No seed = different result each run (default behaviour)

Real Example — Secret Santa Picker

python
import random

people = ["Riya", "Arjun", "Sneha", "Vikram", "Kavya"]

givers    = people[:]          # copy the list
receivers = people[:]
random.shuffle(receivers)

# Re-shuffle if anyone is assigned to themselves
while any(g == r for g, r in zip(givers, receivers)):
    random.shuffle(receivers)

print("🎁 Secret Santa Assignments:")
for giver, receiver in zip(givers, receivers):
    print(f"  {giver:8} → buys for → {receiver}")
output (varies each run)
🎁 Secret Santa Assignments:
  Riya     → buys for → Vikram
  Arjun    → buys for → Kavya
  Sneha    → buys for → Riya
  Vikram   → buys for → Arjun
  Kavya    → buys for → Sneha

"choice(), shuffle(), and randint() cover 95% of everything you’ll ever need from the random module. Learn these three and you can build games, quizzes, and simulations."

— ShurAI

🧠 Quiz — Q1

What does random.randint(1, 6) return?

🧠 Quiz — Q2

What is the difference between random.sample() and random.choices()?

🧠 Quiz — Q3

What does random.shuffle(my_list) return?

🧠 Quiz — Q4

Why would you use random.seed(42)?