Home Blogs All Courses
python c OA ( Online Assessment ) Amazon Web Services (AWS) leetcode dsa algorithms java Machine learning
Get Referral
Login Sign Up

random.seed() in Python: Why It Matters & How Random Numbers Work


Games, OTP generators, lucky draws, shuffling a playlist, splitting data for machine learning — all use random numbers. But what if you need the same "random" numbers every time you run your program, for testing or for reproducible ML results? That is exactly what random.seed() does. In this blog you will understand how random numbers really work in Python and why the seed is so important.

Watch the video first, then use this blog as your notes. Every section has runnable examples, and at the end you will find a "Predict the Output" quiz, practice challenges, a self-check list, interview questions and a quick summary.

How to use this blog: Open the Python shell (type python in your terminal) or any online Python editor and type every example yourself. Change the values, break the code and see what happens — that is how concepts really stick.

1. The random Module — Quick Tour

import random

print(random.random())             # float in [0.0, 1.0)
print(random.randint(1, 6))        # int from 1 to 6 (both included) - a dice!
print(random.randrange(0, 10, 2))  # 0, 2, 4, 6 or 8
print(random.uniform(1, 10))       # float between 1 and 10
print(random.choice(["red", "green", "blue"]))   # one random item
print(random.choices([1, 2, 3], k=5))           # 5 items, repeats allowed
print(random.sample(range(1, 50), 6))           # 6 unique items (lottery!)

cards = [1, 2, 3, 4, 5]
random.shuffle(cards)              # shuffles the list in place
print(cards)
FunctionReturnsExample use
random()Float in [0, 1)Probabilities
randint(a, b)Int from a to b (inclusive)Dice, OTP digits
randrange(start, stop, step)Int from range (stop excluded)Random even number
uniform(a, b)Float between a and bRandom temperature
choice(seq)One itemPick a winner
choices(seq, k=n)n items with replacementSimulations
sample(seq, k)k unique itemsLottery numbers
shuffle(list)None (shuffles in place)Shuffle a deck
seed(x)None (sets the starting point)Reproducible results

Run the code above twice — you get different output each time. Now let's see why.

2. Are Computer Random Numbers Really Random?

No! Computers are deterministic machines. Python uses a Pseudo-Random Number Generator (PRNG) called the Mersenne Twister. It produces a long sequence of numbers that looks random, using a mathematical formula.

seed  --->  [ Mersenne Twister formula ]  --->  n1, n2, n3, n4, ...
(starting value)                                 (looks random, but fully
                                                  decided by the seed)

The seed is the starting value of this formula. Same seed → same sequence of numbers, every single time. If you do not set a seed, Python uses the current system time / OS randomness, so you get different numbers each run.

Analogy: Think of the PRNG as a huge book of random-looking numbers. The seed is the page number you start reading from. Open the same page and you read the same numbers.

3. random.seed() in Action

import random

random.seed(42)
print(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100))
# 82 15 4

random.seed(42)              # reset to the same starting point
print(random.randint(1, 100), random.randint(1, 100), random.randint(1, 100))
# 82 15 4   <-- exactly the same!

Try it: run this program 10 times — the output never changes. Remove the seed line and it changes every time.

random.seed(42)
print(random.random())                       # 0.6394267984578837

random.seed(42)
print([random.randint(1, 6) for _ in range(5)])   # [6, 1, 1, 6, 3]

random.seed(1)
deck = [1, 2, 3, 4, 5]
random.shuffle(deck)
print(deck)                                  # [3, 4, 5, 1, 2]

Note: The exact numbers shown here come from CPython 3. They are guaranteed to be the same every time on the same Python version, but may differ between very different Python versions.

4. Syntax and Parameters

random.seed(a=None, version=2)
ParameterMeaning
aThe seed value: an int, float, str, bytes or bytearray. None (default) uses system time / OS randomness.
versionHow str/bytes seeds are converted (default 2). You almost never change this.
random.seed(100)         # int seed (most common)
random.seed("coding")    # string seeds also work
random.seed()            # back to unpredictable randomness

5. Why is random.seed() Important?

Use caseWhy the seed helps
DebuggingA bug that appears with "random" data can be reproduced exactly
TestingUnit tests get predictable results and do not fail randomly
Machine learningSame train/test split and same model initialisation → results can be reproduced and compared
Scientific simulationsOther researchers can repeat your experiment
GamesSame seed → same generated world (like Minecraft world seeds!)
Teaching / tutorialsStudents see exactly the same output as the teacher
# Machine learning example (scikit-learn)
from sklearn.model_selection import train_test_split

X = list(range(10))
y = [0, 1] * 5
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42   # random_state = the seed
)
# Same split every time you run it

In data science you will also see np.random.seed(42) for NumPy and torch.manual_seed(42) for PyTorch — the same idea.

6. Independent Generators with random.Random

random.seed() changes the global generator used by every part of your program. For cleaner code, create your own generator object:

import random

gen = random.Random(42)        # its own seed, does not affect the global one
print(gen.randint(1, 100))     # 82
print(random.randint(1, 100))  # unaffected by gen

7. Security Warning: Never Use random for Passwords or OTPs

Because the sequence is predictable once the seed is known, the random module is not secure. For passwords, tokens and OTPs use the secrets module:

import secrets

otp = "".join(secrets.choice("0123456789") for _ in range(6))
token = secrets.token_hex(16)
print(otp, token)

8. Mini Project: Reproducible Quiz Shuffler

A teacher wants each student to get the questions in a different order — but the same student must always get the same order. Use the roll number as the seed!

import random

questions = ["Q1: Lists", "Q2: Loops", "Q3: Functions", "Q4: OOP", "Q5: Strings"]

def paper_for(roll_number):
    rng = random.Random(roll_number)    # seed = roll number
    paper = questions[:]                # copy, don't change the original
    rng.shuffle(paper)
    return paper

print(paper_for(101))
print(paper_for(102))
print(paper_for(101))   # identical to the first line - reproducible!

9. Common Mistakes

MistakeWhat happensFix
Calling random.seed(42) inside a loop before every random callYou get the same number every timeSeed once, at the start of the program
Forgetting import randomNameErrorImport the module first
Thinking randint(1, 10) excludes 10Off-by-one bugsrandint includes both ends; randrange excludes stop
Using random for passwordsPredictable secretsUse the secrets module
Assigning x = random.shuffle(l)x is Noneshuffle works in place; just use l

Quiz: Predict the Output

Don't run the code yet! Write your answers in a notebook, then check the Answers section at the end of this blog.

Q1. Will both lines print the same number?

import random
random.seed(5); a = random.randint(1, 10)
random.seed(5); b = random.randint(1, 10)
print(a == b)

Q2. What is the output?

import random
l = [1, 2, 3]
print(random.shuffle(l))

Q3. Can random.randint(1, 6) return 6?

Q4. Can random.randrange(1, 6) return 6?

Q5. What happens?

import random
for i in range(3):
    random.seed(10)
    print(random.randint(1, 100), end=" ")

Q6. Which algorithm does Python's random module use?

Q7. Which module should you use to generate a secure OTP?

Practice Challenges

Solve these from easy to tricky:

  1. Simulate rolling a dice 10 times with seed(7). Run it twice and confirm the results match.
  2. Write a program that picks 3 lucky winners from a list of 10 names using sample.
  3. Generate a random 4-digit number between 1000 and 9999.
  4. Simulate 1000 coin tosses and count heads and tails. Compare results with and without a seed.
  5. Create a secure 8-character password using the secrets module (letters + digits).

Solutions for challenges 2, 4 and 5 are in the Answers section.

Self-Check: Can You Do This?

Tick off each skill once you are confident:

Frequently Asked Interview Questions

Q: What does random.seed() do?
It initialises the pseudo-random number generator so that the sequence of random numbers becomes reproducible.

Q: What happens if you do not set a seed?
Python seeds the generator from the operating system's randomness or the current time, so results differ on each run.

Q: Why is the seed 42 used so often?
Only by convention — a reference to "The Hitchhiker's Guide to the Galaxy". Any number works.

Q: What is random_state in scikit-learn?
The seed used by scikit-learn functions to make results such as train/test splits reproducible.

Q: Is Python's random truly random?
No, it is pseudo-random (deterministic). For cryptographic security use the secrets module or os.urandom.

Summary

  • Python's random module generates pseudo-random numbers with the Mersenne Twister.
  • The seed is the starting point: same seed → same sequence.
  • random.seed(x) makes programs reproducible — vital for debugging, testing and ML.
  • Seed once, at the start; don't reseed inside loops.
  • Use random.Random(seed) for an independent generator.
  • Never use random for passwords or OTPs — use secrets.

Quick Revision Cheat Sheet

ConceptSyntax / Key point
Set seedrandom.seed(42)
Random intrandom.randint(1, 6)
Random floatrandom.random()
Pick onerandom.choice(seq)
Pick k uniquerandom.sample(seq, k)
Shufflerandom.shuffle(lst)
Own generatorrandom.Random(42)
Secure tokensecrets.token_hex(16)

Answers

Quiz answers

  1. True – same seed, same sequence.
  2. None – shuffle changes the list in place.
  3. Yes – both ends are included.
  4. No – the stop value is excluded.
  5. The same number is printed three times, because the seed is reset before each call.
  6. The Mersenne Twister PRNG.
  7. secrets

Challenge solutions

# Challenge 2
import random
names = ["Aman", "Riya", "Karan", "Neha", "Ravi", "Sana", "Ali", "Meera", "Dev", "Isha"]
print(random.sample(names, 3))

# Challenge 4
random.seed(1)
tosses = [random.choice(["H", "T"]) for _ in range(1000)]
print("Heads:", tosses.count("H"), "Tails:", tosses.count("T"))

# Challenge 5
import secrets
import string
chars = string.ascii_letters + string.digits
password = "".join(secrets.choice(chars) for _ in range(8))
print(password)

Conclusion

You now know how Python generates random numbers and how random.seed() makes them reproducible — a skill you will use constantly in testing, data science and machine learning. Next, continue your OOP journey with Encapsulation in Python.

If this blog helped you, watch the full video above, subscribe to the channel and share it with a friend who is learning Python. Happy coding!