Trying to create a unique code for a voucher or a gift card?
If you need a roughly random string of letters or numbers my suggestions are:
- Use all capital letters (reduced confusion from customers when reading your codes).
- Don’t use letters that can be confused with number, so I, L and 1 are out, as well as 0 and O.
- Remove vowels – we don’t want to accidentally create words that might go against our brand. e.g. cuss words.
This leaves us with this list:
characters = 'BCDFGHJKLMNPQRSTVWXYZ23456789'
Python code to create a random code that is twelve letters long is:
import random
code = ''.join(random.choices(characters, k=12))
Now lets add some spacing in the code so it’s not a long line of letters and numbers.
import random
characters = 'BCDFGHJKLMNPQRSTVWXYZ23456789'
code_parts = [''.join(random.choices(characters, k=4)) for _ in range(3)]
code = '-'.join(code_parts)
We get some codes like this:
WFFF-VHJD-B235
Q6R4-DC5Y-GL58
DPM2-878V-M885
S984-C347-3ZBF
H9F6-MKMC-3N5F
Very nice! We can then use those codes and give them to customers who will have no trouble entering them into your application.
Enjoy!