Bite 44 / 4417 views
Programming LanguagesIntermediate

Python random vs secrets: why your password generator is unsafe

random is reproducible from a seed; secrets is designed to be unpredictable.

Reviewed by System Admin · Dec 2, 2026 · Editorial standards

Read time~2 min
FormatVisual
Check1 question
AccessFree

Visual breakdown

Interactive

Python random vs secrets: why your password generator is unsafe — Visual Breakdown

Option A

random module (PRNG)

A
  • ✓Seeded internal state
  • ✓Same seed gives same output
  • ✓Fast, but predictable
  • ✓Fine for games and simulations
Option B

secrets module (CSPRNG)

B
  • ✓Bytes from the OS random source
  • ✓No seed you can set
  • ✓Output cannot be replayed
  • ✓Built for passwords and tokens

The simple version

No jargon

You wrote a password generator with Python's random module. It runs, it prints a password, and it looks fine. But random is a PRNG (pseudo-random number generator). That means it produces a stream of numbers from an internal state. If someone knows or guesses that state, they can reproduce the same stream. In Python, you can set that state yourself with random.seed(). Run the same seed twice, and you get the same "random" password twice. That is the bug: your password is only as secret as the seed. The secrets module does the same job with a different promise. It pulls bytes from the operating system's cryptographic random source. You cannot set a seed. You cannot replay the stream. Two runs give different passwords, and an attacker cannot work backwards from the output to the internal state. The API looks similar: secrets.choice() picks a character, just like random.choice(). Here is a concrete example. With random.seed(42) and a fixed alphabet, two runs produce the same password. With secrets.choice() and the same alphabet, two runs produce different passwords. This is analysis of the general principle, not a claim about any specific tutorial's code. So "working" and "safe" are two different bars. Working means the function returns a string of the right length. Safe means an attacker cannot predict the next string. For a password, the second bar is the one that matters.

Why it matters in production

A password generator that uses random can produce the same password twice if the seed is known or set. That turns a secret into a value an attacker can recompute. The fix is small: swap random.choice for secrets.choice and remove any seeding. The same rule applies to API tokens, session IDs, and password reset links. If the value is supposed to be unguessable, use a cryptographic source. If you are simulating dice or shuffling a playlist, random is fine. The cost is a small speed difference, which does not matter for a password.

Implementation & syntax

Read-only example
🐍Python
import random
import secrets
import string

alphabet = string.ascii_letters + string.digits

random.seed(42)
print(''.join(random.choice(alphabet) for _ in range(12)))
random.seed(42)
print(''.join(random.choice(alphabet) for _ in range(12)))

print(''.join(secrets.choice(alphabet) for _ in range(12)))
print(''.join(secrets.choice(alphabet) for _ in range(12)))

Check your understanding

Quick Concept Check

Test your understanding in 15 seconds

Why is random.seed(42) dangerous in a password generator?

Bite progression44 / 44

Related concepts

Keep going

Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.