I recently posted this question about codes for a gift-card-like voucher that users can redeem online. I wanted to find the best tradeoff between large keyspace, low guessability, and human readability. Now that I'm into implementation I realize I've got another problem altogether, more of an algorithmic challenge.
Let's assume I adopt some code format - say 10 characters from A to Z for simplicity, and I start generating vouchers. What is the correct algorithm to do this?!
My first approach is to number all possible codes from 0 to 308,915,776, then start generating random numbers in that range. This obviously has a big problem though - I have to check my random number against all previously generated voucher codes and if it collides with an existing one I'll have to discard the code and try another. As the system accumulates more data it will slow down. At the extreme when there is only one code left it will be nearly impossible for the system to guess it correctly.
I could pre-generate all codes and shuffle them, then consume them in order. But this means I have to store many codes, and in fact my keyspace is bigger than the one i described, so we're talking about a very large amount of data. So that's also not too desirable.
So this leaves me with using the codes sequentially. I do not want guessable voucher codes though. The user who buys voucher "AAAAAAAAAY" should not have a good chance of getting another valid code if they type in "AAAAAAAAAZ".
I can shuffle my alphabet and my positions so that instead of
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' i use
'LYFZTGKBNDRAPWEOXQHVJSUMIC'
and so that instead of positions
9 8 7 6 5 4 3 2 1 0 the positions are
1 8 0 7 5 4 3 9 2 6
Using this logic, given the code
LNWHDTECMA
the next code would be
LNEHDTECMA
This is definitely way less guessable. But they're still only one character off from each other, and given just two of these vouchers you would know which position is incrementing, and you would have a 90% chance of getting the next code in 24 guesses or less.
My "escape hatch" is to ditch all this and go with GUIDs. They have more characters than I wanted my users to have to type in, and contain similar characters like I/1 and O/0, but they magically make all of the above headaches go away. Still, I'm having fun thinking about this, maybe you are too. I'd love to hear some alternate suggestions. What have you got?
Thanks!
Solve the issue here: If the claim code on your Amazon.com Gift Card is unreadable, please contact us. You’ll need to provide 3 consecutive characters from anywhere in the claim code, other than the first 2 or last 4 characters.
They also ask for the serial card #, looks like there are several layers to recovery although you don't need the card number to redeem just claim code. – Visitation