A customer reads a code off a podcast sponsor's screenshot and types ANNA-I0K1. The real code was ANNA-1OKL. The checkout says the code is invalid, the customer assumes the offer was fake, and nobody ever finds out, because a failed discount code does not generate a support ticket. It generates a closed tab.
Every character in that mix-up is one of five, and removing them costs you almost nothing.
TL;DR: Drop 0, O, 1, I and L from the alphabet. That leaves 31 characters, and six of them still give 887 million combinations, which is more than any program needs. Keep one case, because plenty of checkouts compare codes exactly. Use a readable prefix plus a random tail so the code identifies the partner without being guessable. And draw the characters properly: a naive
byte % 31makes eight characters 12.5% more likely than the other 23. The referral code generator does all four.
Why remove characters from the alphabet?
Because a code is read by a human from one surface and typed into another, and the alphabet is full of pairs that are identical in most fonts.
| Pair | Where it breaks |
|---|---|
0 and O |
Every sans-serif font without a slashed zero |
1, I and l |
Screenshots, print, anything in a proportional font |
5 and S, 8 and B |
Lower-resolution screens and handwriting |
The first two account for nearly all of it. Removing one of each pair, which is five characters, leaves 31 out of 36 and eliminates the failure entirely. The cost is smaller than it sounds:
| Length | Full 36-character alphabet | 31 characters, look-alikes removed |
|---|---|---|
| 6 | 2,176,782,336 | 887,503,681 |
| 8 | 2,821,109,907,456 | 852,891,037,441 |
Eight characters out of a reduced alphabet still gives 852 billion combinations. No referral program in existence needs more than that, and the version that is harder to guess is also the version customers cannot type.
There is one case where this does not apply. If a code will only ever be copied and pasted, from an email or a link, the confusable characters cost nothing and you can keep the full alphabet. The moment a code appears in a video, on a card, on a podcast or on a sticker, it is being typed by hand.
How long should a code be?
Six to eight random characters, and the limit is not guessing.
Guessing a specific partner's code is not the realistic attack. What happens in practice is someone trying a short list of plausible codes: SAVE20, WELCOME10, the brand name with a number. A random tail defeats that entirely at any length past about five characters, because there is nothing plausible to try.
What the length does decide is accidental collisions when you issue a lot of codes. Those follow the birthday problem rather than intuition, which is why the numbers are higher than most people expect:
| Codes issued | Length 6 (887 million) | Length 8 (852 billion) |
|---|---|---|
| 1,000 | 0.06% | effectively zero |
| 10,000 | 5.48% | 0.0059% |
| 100,000 | 99.64% | 0.58% |
At six characters and a hundred thousand codes, a collision is essentially certain. At eight it is still well under one percent at the same volume. If you are issuing codes in the tens of thousands, eight is the length, and the extra two characters cost nothing to paste and very little to type.
That table assumes the duplicate matters. Whether it does depends on your system: if the code is a unique key, a collision is an error at import, which is annoying and safe. If it silently attributes two partners' sales to one of them, it is a quiet accounting problem, which is worse.
What is the bug most code generators have?
Modulo bias, and it is invisible in the output.
The obvious way to pick a random character is to take a random byte and use the remainder: alphabet[byte % 31]. A byte holds 256 values, and 256 divided by 31 is 8 with 8 left over. So the first eight characters of the alphabet come up nine times in every 256 draws and the other twenty-three come up eight times. That makes those eight characters 12.5% more likely than the rest.
You will never see this in a batch of codes. It does not produce duplicates, it does not look patterned, and every code still passes a glance. It is only visible across tens of thousands of draws, which is exactly when a program is big enough for it to matter.
The fix is rejection sampling: discard any draw that lands in the uneven tail and draw again. It costs a handful of extra draws per thousand characters and the distribution becomes exactly uniform. The generator here does that, and the test behind it draws 6,200 codes and fails if any character runs away with the rest.
The same reasoning rules out Math.random() for anything where the code has value. It is not a cryptographic source and its output is predictable from previous values in some engines. crypto.getRandomValues is available in every browser and costs nothing extra.
Should a code be readable or random?
Both, in that order, and the split is what makes each half work.
A prefix identifies the partner and is what gets promoted: ANNA-X7K2P4 is a code a listener remembers long enough to type. A random tail means knowing one of Anna's codes tells you nothing about the next one. A readable code alone, ANNA20, is guessable by anyone who knows a second partner is called Ben.
Four formats cover almost every program:
| Format | Example | Use |
|---|---|---|
| Name plus random | ANNA-X7K2P4 |
Individual partners and creators |
| Offer plus random | SUMMER-K4M2 |
A campaign with a fixed discount |
| Channel plus random | POD-9QR3T7 |
When you want attribution readable off the code |
| Pure random | H4K2P9RM |
When the code should reveal nothing |
Pick one and keep it. Mixed formats are difficult to reconcile later, and a code whose format tells you nothing is one you cannot sort in a spreadsheet six months on.
Code or link?
Give every partner both, built from the same identity.
A link converts better, because there is nothing to remember and nothing to type. It carries the code in a query parameter your tracker reads, and it is what a partner puts in a newsletter or a description.
A code is what works where a link cannot go: a podcast read, a printed insert, a video, a conversation. It is also the only one that survives a customer who hears about you on Tuesday and buys on Friday from a different device, which is the case where click attribution quietly fails.
Using the same string for both means one row in the spreadsheet and one number to reconcile. Using different ones means two.
One detail on the link: keep whatever parameters the landing page already carries rather than replacing them. A partner link built onto a URL that already has campaign parameters should append, not overwrite, or you lose the campaign attribution you set up separately.
Where does uniqueness actually get enforced?
Not in the generator, and it is worth being precise about this because it is the thing people assume.
A generator produces codes that are distinct from each other within a batch. It has no memory of what you issued last month, and the one on this site has no server at all, so it could not know even if it wanted to. Whatever stores your codes is the system that must reject a duplicate: an affiliate platform, a Shopify discount list, a database column with a unique constraint.
That is the right place for it. The generator's job is to produce codes with enough entropy that collisions are rare; the store's job is to guarantee they never happen. If your store has no unique constraint on the code column, that is worth fixing before worrying about code length.
Once the codes exist, the terms attached to them belong in writing. What an affiliate is paid for, the attribution window and the refund clawback go in an affiliate agreement, and what the commission actually costs you is arithmetic the commission calculator will do.



