How to Get the Same Random Numbers Every Run in Python with random.seed
A script that uses random numbers gives a different result on every run, which makes it hard to check, to debug or to show to someone else. random.seed fixes the starting point of Python's random numbers, so every run produces the same ones. This guide does it on Python 3.15.2.
Syntax
import random
random.seed(<number>)
| Part | What it means |
|---|---|
random.seed(...) | Sets the starting point of the random module's numbers. Call it once, before the first random number |
<number> | Any whole number. The same number always gives the same series of random results |
The Python documentation says random.seed will "Initialize the random number generator", and that "If a is omitted or None, the current system time is used." Its notes on reproducibility add: "By reusing a seed value, the same sequence should be reproducible from run to run as long as multiple threads are not running."
The Example
A script rolls a die five times. With a seed of 7, it prints the same five rolls on every run. Without the seed, every run prints new ones.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
import random
random.seed(7)
print(random.choices([1, 2, 3, 4, 5, 6], k=5))

random.seed(7) comes before the first random pick. random.choices then picks five numbers from 1 to 6, like five rolls of a die.
Step 2: Run It Twice
uv run python dice.py
[2, 1, 4, 1, 4]
Run the same command again:
uv run python dice.py
[2, 1, 4, 1, 4]

Both runs rolled 2, 1, 4, 1, 4. The rolls still come from the random number generator, but the seed made it start from the same point both times. The commands work unchanged in PowerShell.
Step 3: Take the Seed Out
To see what the seed changed, delete the random.seed(7) line and run the script twice more:
uv run python dice.py
[2, 4, 2, 3, 3]
uv run python dice.py
[6, 3, 1, 3, 1]

Now each run gives its own rolls. Without a seed, Python picks a new starting point every time the script starts.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling random.seed(7) inside a loop | Three turns of a loop that seeded and then rolled once gave [2], [2] and [2]: every turn started again from the same point | Seed once, before the loop. Then the three turns gave [2], [1] and [4] |
| Expecting the same numbers from a second call without seeding again | After one random.seed(7), the first five rolls were [2, 1, 4, 1, 4] and the next five [3, 1, 4, 1, 3] | That's how it should work: the series carries on. Call random.seed(7) again to start it over |
| Passing a list as the seed | random.seed([7]) raised TypeError: The only supported seed types are: None, int, float, str, bytes, and bytearray. | Pass a whole number |
Using a seeded random for passwords or tokens | Anyone who knows the seed gets the same "random" values | Use the secrets module. The documentation warns: "The pseudo-random generators of this module should not be used for security purposes." |
FAQ
Which number should I use as the seed?
Any whole number works, and none is better than another. A different number gives a different series: with random.seed(8), the five rolls were [2, 6, 1, 5, 1]. What matters is that you keep using the same one.
Can the seed be text?
Yes. random.seed("lesson") ran without an error and gave [5, 4, 3, 1, 1] on that seed. The documentation says a string "gets converted to an int and all of its bits are used".
How do I get different numbers again?
Delete the random.seed(...) line, as Step 3 did, or call random.seed() with nothing in the brackets. After random.seed(), the script rolled [1, 5, 6, 6, 1], a fresh series.
Will another computer get the same numbers?
With the same seed, the same code and the same Python version, yes. Across Python versions it's less certain. The documentation says "Most of the random module's algorithms and seeding functions are subject to change across Python versions", and promises only that the basic random() method keeps giving the same series for the same seed.
Conclusion
With random.seed(7) before the first pick, dice.py rolled [2, 1, 4, 1, 4] on every run. Without it, two runs rolled [2, 4, 2, 3, 3] and [6, 3, 1, 3, 1].
Additional Resources
- random.seed — the function and the values it accepts.
- Notes on Reproducibility — what stays the same across Python versions.
- secrets — random values for passwords and tokens.
