Need a number, a dice roll, or a random pick from a list? Three common scenarios on one page.
Computers are deterministic, so they cannot produce randomness on their own. Instead they run a pseudo-random number generator: an algorithm that turns a starting value — the seed — into a long sequence that looks random. Given the same seed, it produces exactly the same sequence, which is a feature for reproducibility and a hazard for security.
| Type | Seeded from | Fit for |
|---|---|---|
Standard PRNG Math.random() | A clock value or similar | Shuffling a playlist, placeholder data, animations |
| Cryptographically secure PRNG | An entropy source provided by the operating system | Keys, passwords, tokens, session IDs |
Once an attacker knows or guesses the seed, the entire sequence follows. Seeds drawn from the system clock have only a few thousand plausible values, which is a trivial search. Standard generators can also be reverse-engineered from a handful of consecutive outputs. Neither property matters for a dice roll and both are fatal for a password. Use the platform's secure API — crypto.getRandomValues() in the browser, secrets in Python, crypto.randomBytes() in Node — for anything security-related.
Mapping a random integer onto a smaller range with a naive modulo introduces bias: some outcomes become slightly more likely than others. The effect is small but real, and noticeable when drawing from a large range into a small one. Correct implementations either reject out-of-range values or use a rejection-free technique. If a generator produces visibly uneven distributions across many runs, this is usually why.
Math.random().