Lazo Lab Sign up

Cryptography / Crypto in practice

Lesson 20 of 25

Random numbers

Crypto depends on unpredictable randomness. Use secrets in Python or crypto.getRandomValues in JavaScript, never Math.random or random for keys and tokens.

Key points

  • secrets.token_hex(32)
  • crypto.getRandomValues() in browsers
  • Predictable randomness breaks everything
import secrets
print(secrets.token_urlsafe(32))
Watch a video on thisOpens YouTube search results for “Random numbers” in a new tab

Quiz · +10 XP

Which is right for generating a secret token in Python?

Log in to save progress and earn XP.