When developers need a unique string, the reflex is often to reach for a UUID. And UUIDs are excellent for what they're designed for: stable, globally unique identifiers for database records and distributed systems. But a UUID is 36 characters of hex and hyphens in a fixed format. That's not always what you want. Sometimes you need a short invite code. Sometimes an API key. Sometimes a random filename, a CSRF token, or a test dataset of names and values. For those cases, a configurable random string generator is the right tool.
What a Random String Generator Does
A random string generator produces a string of a specified length drawn from a specified character set. You control the length, the alphabet (uppercase, lowercase, digits, symbols, or a custom set), and how many strings to generate. The Random String Generator on DevToolShack runs entirely in your browser using crypto.getRandomValues() — the same cryptographically secure source used for password generation — so nothing is sent to a server.
UUID vs Random String: Choosing the Right Tool
The choice between a UUID, a Nano ID, and a custom random string comes down to three questions: Does it need to be globally unique without a central coordinator? Does length or character set matter? Does it need to be URL-safe?
Use a UUID when you need a universally unique identifier that can be generated on any machine without a database check. Database primary keys, record identifiers in distributed systems, and file identifiers all fit this mold. UUIDs have a well-understood collision probability and a standard format that libraries and databases handle natively. For a full comparison of UUID versions and when to use each, see The Developer's Guide to UUID vs Nano ID.
Use a Nano ID when you want UUID-level uniqueness in a shorter, URL-safe format. Nano IDs are 21 characters by default and use a URL-safe alphabet, making them suitable for IDs in URLs, slugs, and anywhere compactness matters. The Nano ID Generator on DevToolShack generates these instantly.
Use a random string generator when the specific character set, length, or format matters more than strict uniqueness guarantees. The use cases are different from identifiers.
Common Use Cases for Custom Random Strings
Invite and Redemption Codes
Short codes that users type manually benefit from a restricted alphabet that avoids visually ambiguous characters. The characters 0, O, 1, I, and l are notoriously hard to distinguish in many fonts. A 8-character code from an alphabet of ABCDEFGHJKLMNPQRSTUVWXYZ23456789 (excluding the ambiguous ones) is easy to transcribe and has over 1.6 trillion possible values.
API Keys and Secret Tokens
A 32-character hex string or a 40-character alphanumeric string makes a solid API key or webhook secret. These are never typed by hand, so ambiguous characters aren't a concern — maximizing the character set increases entropy. A 32-character string from all 62 alphanumeric characters has about 190 bits of entropy, which is more than sufficient for any authentication token. Keep in mind that for stored secrets (passwords, session tokens), the string should be passed through BCrypt or Argon2 before storage. Random strings from a generator are suitable for tokens that are compared directly, like API keys or CSRF tokens.
CSRF Tokens
CSRF tokens need to be unpredictable, unique per session, and long enough to be infeasible to guess. A 32-character alphanumeric string generated with a CSPRNG (cryptographically secure pseudo-random number generator) is standard. crypto.getRandomValues() in the browser and crypto.randomBytes() in Node are both CSPRNGs. Math.random() is not — never use it for security-sensitive tokens.
Test Data and Seed Data
When populating a test database or building a demo, generating batches of random strings quickly populates name fields, reference codes, and slugs without needing a fixture file. The Random String Generator supports bulk generation — you can generate 50 or 100 strings at once for exactly this kind of use.
Temporary File Names and Upload Tokens
When accepting file uploads, storing the file under its original name is a security risk (path traversal, overwriting existing files, executable uploads). Generating a random string as the storage filename and keeping the original name in a database record is standard practice. 16 to 24 alphanumeric characters is more than enough for this purpose.
How Random Strings Are Generated Securely
The key is using a cryptographically secure random number generator rather than Math.random(). In browsers, crypto.getRandomValues() fills a typed array with random bytes from the OS entropy pool. You can use it to generate a random string from a custom alphabet like this:
function randomString(length, alphabet) {
const chars = alphabet.split('');
const bytes = crypto.getRandomValues(new Uint8Array(length));
// Use modulo to map bytes to the alphabet
// Note: for an unbiased result, use rejection sampling
// when alphabet length doesn't divide evenly into 256
return Array.from(bytes, b => chars[b % chars.length]).join('');
}
const token = randomString(32, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
console.log(token); // e.g. "k7Rp2mXvNqLsYdFwJtCaEgHuBiOz4K8n"
b % chars.length approach introduces a slight statistical bias when the alphabet size doesn't divide evenly into 256. For most non-security use cases this is negligible, but for security tokens you should use rejection sampling or a library that handles this correctly. Nano ID's source is a good reference implementation.Entropy: How Random Is Random Enough?
Entropy is the measure of how unpredictable a string is, expressed in bits. The formula is simple: log2(alphabetSize) × length. Some reference points:
- 8 digits (PIN): ~26 bits — brute-forceable in milliseconds
- 8 alphanumeric characters: ~47 bits — weak for security
- 16 alphanumeric characters: ~95 bits — reasonable for short-lived tokens
- 32 alphanumeric characters: ~190 bits — strong for API keys
- UUID v4: 122 bits of randomness — solid for identifiers
For anything used as a security token, aim for at least 128 bits of entropy. For user-typed codes where length is a UX constraint, the math above helps you choose the minimum viable alphabet size.
Generating Random Numbers
If you need random numbers rather than strings — for test data, sampling, or simulation — the Random Number Generator on DevToolShack generates numbers within a specified range, with optional bounds and bulk output. It also uses crypto.getRandomValues() under the hood.