Random String Generator

Generate random strings from exactly the characters you choose, for coupon codes, test fixtures, invite tokens or filenames. You get one string per line, up to 10,000 at a time, drawn from the browser's cryptographic random generator.

Random Strings

1 Random String Generator example

Settings

Length:     12
Count:      3
Sets:       A-Z, a-z, 0-9
Exclude:    I l 1 | O 0 o
Unique:     on
Entropy:    ~69.7 bits per string

Output

k7Rm2QxW9pLc
Bv4nT8zYh3Fs
Jd6wXq2Nr9Ke

2 Each option changes the alphabet or the batch

Character sets and Custom

The ticked sets and anything typed into Custom merge into one alphabet with duplicates removed. Each character in that alphabet is equally likely at every position.

Exclude ambiguous

Drops I, l, 1, |, O, 0 and o. Use it for anything read aloud, printed on a card or typed from a screenshot. It costs a little entropy per character.

Unique only

No string appears twice in the list, because the tool redraws duplicates. That makes the batch slightly less random than independent draws. If you ask for more unique strings than can exist, the tool says so instead of generating.

No "at least one of each"

The tool does not force every set to appear. For example, a 6-character string from A-Z and 0-9 can come out all letters. If a system demands at least one digit, use the password generator or add length.

3 Size the string by its entropy

Randomness comes from crypto.getRandomValues(), and each character is picked with rejection sampling: the tool discards a 32-bit value that falls in the uneven tail above the last full multiple of the alphabet size. That removes the small bias a plain % length introduces.

The header shows the entropy per string, length × log2(alphabet size). For a rate-limited one-time code, 30 to 40 bits is plenty. Give bearer secrets 128 bits. An API key or a password reset token works as a secret on its own, and 22 characters of A-Za-z0-9 reach 128 bits. Short strings from a small alphabet run out fast, so check the count against the space before you rely on Unique only.

How to use the Random String Generator tool

  1. Set Length (up to 1024) and Count (up to 10,000). The list updates as you type.
  2. Tick the sets to draw from: A-Z, a-z, 0-9 and Symbols. Add anything else, such as accented letters, in the Custom box.
  3. Tick Exclude ambiguous to drop I l 1 | O 0 o, and Unique only if no string may repeat in the batch.
  4. Check the alphabet size and entropy in the panel header, then use Copy or Download (random-strings.txt). Generate draws a new batch.

How to generate a random string in JavaScript, Python and PHP

JavaScript (browser or Node.js)

function randomString(length, alphabet) {
  const chars = Array.from(alphabet);
  const n = chars.length;
  // Largest multiple of n below 2^32: values above it would bias the result
  const limit = Math.floor(0x100000000 / n) * n;
  const buf = new Uint32Array(1);
  let out = "";
  while (out.length < length) {
    crypto.getRandomValues(buf);
    if (buf[0] < limit) out += chars[buf[0] % n];
  }
  return out;
}

const ALNUM = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
console.log(randomString(16, ALNUM));

Never use Math.random() for anything that must be hard to guess. This snippet makes one getRandomValues() call per character for clarity; fill a larger array at once for speed. out.length counts UTF-16 units, so keep the alphabet in the BMP.

Python

import secrets
import string

alphabet = string.ascii_letters + string.digits
print("".join(secrets.choice(alphabet) for _ in range(16)))

# Unique batch
batch = set()
while len(batch) < 100:
    batch.add("".join(secrets.choice(alphabet) for _ in range(8)))
print(len(batch))

# URL-safe shortcut, length is about 1.3 x nbytes
print(secrets.token_urlsafe(16))

Use secrets, not random. The random module is a Mersenne Twister and its output can be predicted after enough samples.

PHP

<?php
function randomString(int $length, string $alphabet): string
{
    $chars = mb_str_split($alphabet);
    $max = count($chars) - 1;
    $out = '';
    for ($i = 0; $i < $length; $i++) {
        $out .= $chars[random_int(0, $max)];
    }
    return $out;
}

echo randomString(16, 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'), PHP_EOL;

// PHP 8.3+: Random\Randomizer does the same in one call
// echo (new Random\Randomizer())->getBytesFromString('abcdef0123456789', 16);

random_int() is unbiased and cryptographically secure. str_shuffle() and rand() are neither.

Common errors and how to fix them

Only N different strings exist with this length and alphabet
Unique only is on and the count exceeds alphabet size to the power of length, for example 11 unique one-digit strings. Increase the length or add character sets.
Pick at least one character set
Every checkbox is off and Custom is empty, or Exclude ambiguous removed the only characters left. Tick a set or type characters into Custom.
Some characters appear more often than others in my own code
That is modulo bias from random % alphabet.length. Use rejection sampling as in the JavaScript snippet, or a library call such as secrets.choice() or random_int() that does it for you.
Symbols break the string when it is pasted into a shell, URL or CSV
Characters like $, &, ; and quotes have meaning there. Untick Symbols or restrict Custom to - and _ for strings that travel through URLs, filenames or command lines.

Good to know

Compare strings by entropy, not length. Entropy is length x log2(alphabet size), so 16 characters from 0-9 is 53 bits while the same 16 from A-Za-z0-9 is 95 bits. For a bearer secret, aim for at least 128 bits.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

How do I generate a random string?

Set the length and count, tick the character sets you want, and the list appears one string per line. Copy or Download takes it from there; Generate makes a fresh batch.

Are these strings cryptographically secure?

They come from crypto.getRandomValues(), the browser's CSPRNG, with rejection sampling so every character is equally likely. Whether a string is strong enough depends on its entropy, which is shown in bits above the list.

What does Unique only do?

It guarantees no string appears twice in the batch by redrawing duplicates. If you ask for more unique strings than the length and alphabet allow, the tool says so instead of looping forever.

Which characters count as ambiguous?

I, l, 1, |, O, 0 and o, the characters people misread when copying from a screen or a printed card. Excluding them removes those from every set, including your custom characters.

Does every string contain at least one digit or symbol?

No. Each position is drawn independently from the combined alphabet, so a short string can miss a set entirely. Use the password generator when a system enforces composition rules.

Related

For IDs with a known collision budget, use the Nano ID generator or the UUID generator. For credentials, use the password generator or the API key generator.

More Generators

Browse all generators

Copied to clipboard!