How can I generate random alphanumeric strings?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Random alphanumeric strings are used for tokens, invite codes, temporary passwords, and identifiers. The correct implementation depends on whether the string is security-sensitive. For anything exposed to users or attackers, a cryptographically secure random source matters more than the string-building loop itself.
Decide Whether the Use Case Is Security-Sensitive
A log correlation ID and a password-reset token are not the same problem. If the string can grant access, predictability matters and you should use a cryptographically secure generator.
That means the first engineering choice is not uppercase versus lowercase. It is whether the randomness source must resist guessing.
Generate a Secure String in Python
In Python, the secrets module is the right default for security-sensitive values.
This is appropriate for tokens and other externally visible identifiers where predictability would be a problem.
Generate a Secure String in JavaScript
In browser or modern JavaScript environments, prefer cryptographic APIs over Math.random() when the value matters for security.
For Node.js, the same idea applies through the crypto module.
Know the Tradeoff Between Simplicity and Uniformity
A quick modulo operation such as bytes[i] % alphabet.length is common and often acceptable for application-level token generation, but it introduces slight distribution bias when the alphabet size does not divide evenly into 256.
For many everyday uses, that bias is negligible. For stricter applications, rejection sampling gives a more uniform distribution. The important point is to know which level of rigor the system actually needs.
Avoid Math.random() for Secrets
Math.random() is fine for toy examples, shuffling UI colors, or non-security simulations. It is the wrong choice for password reset links, session tokens, or codes that an attacker might try to predict.
This code works functionally, but not with the security properties you want for sensitive workflows.
Choose the Length Based on Risk
Short strings may be convenient but easier to brute-force. The right length depends on the alphabet size, rate limits, token lifetime, and whether the string is public-facing.
As a rough principle, increasing length is often simpler and safer than inventing clever extra encoding rules. The more important the token, the less reason there is to keep it short just for aesthetics.
Keep Storage and Comparison Safe Too
Generation is only part of the story. If the string is security-sensitive, think about how it is stored, how long it remains valid, and how comparisons are performed. A strong random generator cannot rescue a system that keeps long-lived reset tokens forever or logs them in plain text.
Common Pitfalls
- Using
Math.random()for security-sensitive values creates predictable tokens. - Choosing the character set first and the randomness source second focuses on the wrong problem.
- Generating strings that are too short for the threat model makes brute-force attacks easier.
- Treating token generation as the whole security problem ignores storage, expiration, and logging concerns.
- Reusing one helper for both cosmetic randomness and security-critical tokens can hide important differences in requirements.
Summary
- Start by deciding whether the random string is security-sensitive.
- Use cryptographically secure randomness for tokens, passwords, and user-facing secrets.
- In Python,
secretsis the right default; in JavaScript, use cryptographic APIs instead ofMath.random(). - Pick the string length based on risk, not only on aesthetics.
- Treat generation, storage, expiration, and comparison as one design problem.

