How to Generate Random Passwords (Secure Methods)
March 22, 2026 · 8 min read
You need a strong password. Maybe you're creating a database credential, generating an API key, or just signing up for yet another service. You know "password123" won't cut it — but how do you actually generate a password that's cryptographically secure?
This guide covers the right way to generate random passwords in JavaScript, Python, and the command line, explains the math behind password strength, and shows you why Math.random() is never safe for security.
Why Math.random() Is Not Secure
The most common mistake: using Math.random() for password generation. Here's why that's dangerous:
- Predictable — Math.random() uses a PRNG (pseudo-random number generator) seeded from a predictable source. An attacker who knows the seed can reproduce every "random" value.
- Not uniform — the distribution has subtle biases that reduce effective entropy.
- State can be leaked — in V8 (Chrome/Node.js), the internal state of Math.random() can be reconstructed from a few outputs.
Instead, use a cryptographically secure random source:
| Language | Insecure | Secure |
|---|---|---|
| JavaScript (browser) | Math.random() | crypto.getRandomValues() |
| JavaScript (Node.js) | Math.random() | crypto.randomBytes() |
| Python | random.choice() | secrets.choice() |
| Go | math/rand | crypto/rand |
JavaScript: Secure Password Generator
Browser (Web Crypto API)
Node.js
Python: Secure Password Generator
Command Line Methods
Understanding Password Entropy
Password strength is measured in bits of entropy — the number of possible combinations expressed as a power of 2:
| Charset | Size | Bits per char | 12-char entropy | 16-char entropy |
|---|---|---|---|---|
| Digits only | 10 | 3.32 | 39.9 bits | 53.2 bits |
| Lowercase | 26 | 4.70 | 56.4 bits | 75.2 bits |
| Alphanumeric | 62 | 5.95 | 71.5 bits | 95.3 bits |
| All printable ASCII | 95 | 6.57 | 78.8 bits | 105.1 bits |
General guidelines:
- 64+ bits — minimum for online accounts (rate-limited by server)
- 80+ bits — good for most purposes
- 128+ bits — strong enough for encryption keys, master passwords
A 16-character password using mixed case + digits + symbols gives you ~105 bits — very strong.
Passphrases: The Better Alternative
Random character passwords are strong but hard to remember. Passphrases — random word combinations — offer high entropy with better memorability:
With a 7,776-word list (standard Diceware), each word adds ~12.9 bits of entropy. A 5-word passphrase gives ~64.6 bits — equivalent to a random 10-character mixed-case alphanumeric password, but much easier to type and remember.
Password Generation Mistakes to Avoid
- Using Math.random() or random.random() — these are predictable. Always use crypto-grade randomness.
- Modulo bias — randomByte % charsetLength introduces slight bias when the charset doesn't evenly divide 256. For most passwords this is negligible, but for high-security use, implement rejection sampling.
- Hardcoding seeds — never seed a PRNG with a fixed value or timestamp for password generation.
- Insufficient length — 8-character passwords are crackable with modern hardware. Use 16+ characters.
- Generating in the wrong layer — generate passwords server-side when possible. Client-side generation in JavaScript is fine for tools (like ours), but API keys and database passwords should be generated on the server.
- Logging passwords — never log, print, or store generated passwords in plaintext after use.
Customizable length, character sets, and passphrase mode — all generated in your browser, nothing sent to a server.
Open Password Generator
Related Reading
- How to Use Regex for Email Validation — another common security-adjacent coding task
- How to Decode JWT Tokens — understand token-based authentication
- How to Calculate Chmod Permissions — Unix security fundamentals
- Security Tools — password generators, hash tools, AES encryption, and more