actually I am stuck with the following business case and do not have an idea how to solve it.
I have to create more than 5.000.000 unique alphanumeric codes.
The rules for the codes are:
length: 12
format: every 4 digits "-"
some letters should be excluded like: O or l
The codes should be "secure" (i.e totally random) and it should be possible to run the script multiple times in case the codes aren't enough and we have to create more codes.
e.g. ab4D-406a-BCh7-TEs3
I have to solve this in Python 3.
My first idea was to save the codes into an database and just create them with the random function ASCII-Code -> Letter but maybe the scirpt creates the same code twice so I habe to check every time if that code already exists in the database which will cause a lot of database traffic.
My second idea is to use a hash function, but I think the codes wouldn't be secure and there are no hashfunctions which pass my rules.
My third idea is to use somethink like a random module from python to create the code and write the codes into an file and check the file every time if the code is already inside. But that's also not good for performance but I think betther than using a database.
Anybody an idea how to solve that problem with high performance?
Greetings.
Edit:
I tried this but it takes hours to create the codes. Some tipps how to increase the performance?
import random
sequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
seq = list(sequence)
codelist = []
counter = 0
while len(codelist) < 5000000:
code = ""
counter = counter +1
print(counter)
while len(code) < 12:
code = code + str(random.choice(seq))
try:
codelist.index(code)
except ValueError:
codelist.append(code)
file = open('codefile.txt','w')
for item in codelist:
file.write("%s\n" % item)