<p>I'm working with a client that needs to generate millions of the alphanumeric codes used in magazine scratch-off cards, bottlecap prizes, and so on. They have to be short enough to print on a cap, they want to make sure that ambiguous characters like 1 and I, 0 and O, etc. are not included, and they have to be explicitly stored for future use -- we can't just have an algorithm that determines 'validity' when someone tries to redeem one. Finally, they want to make sure that the codes are randomly distributed inside of a large "code space" so that people can't just guess additional codes by walking through the alphabet.</p>

<p>Are there any pointers towards reasonably efficient algorithms for generating these kinds of code sets? I've scratched a few out on the back of an envelope, but this problem smells like a trap for the unwary.</p>

Let's suppose you can use a character set of, say, 40 symbols of unambiguous upper,lower and numeric characters.
For a sequence of n chars, you've got 40
n
combinations
40
4
= 2,560,000
40
5
= 102,400,000
40
6
= 4,096,000,000
40
7
= 163,840,000,000
40
8
= 6,553,600,000,000
Thus 8 chars gives a pretty good space to work in - if you generated 10 million codes, you'd have to try hundreds of thousands of combinations to brute force a code.
Or you come at from the other direction - give the number of
possible
codes, how many codes
should
you generate to avoid the trap they call the
Birthday Paradox
?
Taking the 8 char code, 6,553,600,000,000 is approx 2
42
, thus you might reasonably generate 2
21
codes from it, or 2,097,152