For the company I worked for, we created a "coupon code" generator, that allowed us to generate millions of self-validating codes, without the expense of loading them in a database.
The codes were calculated as follows:
Create and store a salt of 16 characters, or more.
Loop a counter from 0 to the number of codes - 1. For each counter:
a. generate a hash of the counter and the salt, say md5(counter + salt)
b. generate the coupon code as the counter plus the hash value
Then when a coupon code is received, we extract the counter, and hash the counter + salt, and compare it to the hash found in the code. If they are the same, we know the code is valid.
If a sale is generated using the code, we then record its use in the table, so it can't be used again.
For print media, we generated coupon codes using uppercase base-32 values (2-9,A-H,J-N,P-Z).
For emails and URLs, we used modified base-64 values (see urlsafe_b64encode())
Unfortunately, I can't provide the actual code, as it's under NDA. Good luck!