How Random Number Generators Work (True vs Pseudo)
Random number generators are either pseudorandom or truly random. Learn the difference, when each is safe to use, and how to generate numbers in a range.
A random number generator produces numbers with no predictable pattern, using either a pseudorandom algorithm or a truly random physical source. The difference matters for security.
Pseudorandom vs true random
- Pseudorandom (PRNG) — a fast algorithm that expands a starting "seed" into a stream of numbers. Same seed, same sequence — great for games and simulations, not for secrets.
- True random (TRNG/CSPRNG) — draws from unpredictable physical noise. Browsers expose this via
crypto.getRandomValues, which is safe for passwords, tokens and keys.
Getting a number in a range
To pick a number from 1 to N without bias, generate random values and reject any that fall outside a clean multiple of N. Naive modulo (% N) slightly favors lower numbers.
When it matters
Use a cryptographic source for anything security-related — lottery draws, passwords, tokens. A basic PRNG is fine for shuffling a playlist or a dice roll.
Generate now
Pick numbers in any range with the random number generator. For secrets specifically, use the token generator.