Generating a password sounds trivial โ pick some random characters and you are done. But the word "random" hides a critical security distinction. The function most developers reach for, Math.random(), is not safe for anything security-related. This article explains why, and shows the correct way to generate passwords in the browser.
Why Math.random() is dangerous for passwords
Math.random() is a pseudo-randomnumber generator optimized for speed, not unpredictability. Its output is deterministic given the internal state, and researchers have shown that the state can sometimes be recovered from a handful of outputs. If an attacker can predict your "random" values, they can predict your passwords.
The right tool: the Web Crypto API
Browsers ship a cryptographically secure random number generator: crypto.getRandomValues(). It draws from the operating system's entropy pool and is suitable for keys, tokens, and passwords.
function securePassword(length, charset) {
const values = new Uint32Array(length);
crypto.getRandomValues(values); // cryptographically secure
let out = "";
for (let i = 0; i < length; i++) {
out += charset[values[i] % charset.length];
}
return out;
}This is the exact approach used by our Password Generator, which runs entirely in your browser โ generated passwords are never transmitted anywhere.
A note on modulo bias
The % charset.length trick introduces a tiny statistical bias when the charset size does not evenly divide 2ยณยฒ. For passwords of normal length and a typical character set, this bias is negligible. If you are generating high-stakes cryptographic material, use rejection sampling to eliminate it entirely.
How long should a password be?
Length beats complexity. Each additional character multiplies the number of possible combinations. A practical guideline:
- 8 characters: weak โ crackable quickly.
- 12โ15 characters: acceptable for low-risk accounts.
- 16+ characters: strong for important accounts.
- 20+ with mixed types: effectively uncrackable today.
Best practices
- Use a unique password for every account.
- Store them in a password manager (Bitwarden, 1Password, KeePass).
- Enable two-factor authentication wherever possible.
- Never reuse a password across sites โ one breach should not cascade.
Frequently asked questions
Is generating passwords in the browser safe?
Yes, as long as the tool uses crypto.getRandomValues() and runs client-side. The password is created on your device and never sent over the network.
Are passphrases better than random passwords?
A long, randomly chosen passphrase (several unrelated words) can be both strong and memorable. For accounts stored in a password manager, however, a long random string is simplest and strongest.