Creating a never repeating random hex generator

My company asked me to develop a 4-byte random hex generator for some of the safety equipment we have, but I'm new to this. (To clarify, each code consists of 4 hexadecimal digits).

We currently have a list of 124 hexadecimals that are used, and I need to be able to not repeat these hexadecimal, as well as those that will be generated using this program. I developed a graphical interface and generator, I just need help so that it never repeats.

This is what I still have:

# random hexadecimal generator engine text = "" for i in range(4): rand = random.choice('0123456789abcdef') text = text + rand print(text) # creating the window root = Tk() # GUI window root.title("Hex Generator") root.geometry("250x150") app = Frame(root) app.grid() label = Label(app, text="Click to generate a Hexadecimal") button1 = Button(app, text="Generate") label.grid() button1.grid() label1 = Label(app, text=text) label1.grid() # kicking off event loop root.mainloop() 
0
python random
source share
1 answer

In your question, the question arises about a 4-byte random hex generator, but in the comments you clarify that you only need four hexadecimal digits, which means that there are only 2**16 combinations. This complicates the task: we simply create a list of 65,536 combinations and shuffle it, and then we can simply iterate over this shuffled list. To save a little time and RAM, I create a list of integers and simply convert the integers to hexadecimal strings as needed.

The fact that you have a list of 124 codes that are already in use adds a bit of complexity, but we can handle this by putting these codes in a set; we can easily test the generated codes to see if they are in the used set.

Obviously, we want to be able to run the program several times, so we save the index number of the shuffled list in a text file. This is simpler and more efficient than storing every number that we generate in the used set.

We also need randomization to be consistent, so we need to provide the seed to the random number generator.

Here is the Python 3 code. It can be adapted to work in Python 2 (by changing FileNotFoundError to IOError ), but you cannot switch versions between runs because the sequence of random numbers generated by Python 2 will not be the same as the one created by Python 3

 from random import seed, shuffle passphrase = 'My secret passphrase' seed(passphrase) # Codes that were already in use. # This list could be read from a file instead of being embedded in the script used = '''\ 2b2d 40a7 c257 d929 c252 5805 2936 8b20 ''' # Convert to a set of strings used = set(used.splitlines()) # The index of the next number to generate is stored in this file fname = 'nextindex.txt' try: with open(fname) as f: idx = int(f.read()) except FileNotFoundError: idx = 0 print('Using index', idx) allnums = list(range(0x10000)) shuffle(allnums) #Search for the next code that not in the `used` set while True: hexcode = format(allnums[idx], '04x') idx += 1 if hexcode not in used: print('Code:', hexcode) break # Save the next index with open(fname, 'w') as f: print('Saving index', idx) f.write(str(idx)) 

output from 3 runs

 Using index 0 Code: d0fc Saving index 1 Using index 1 Code: d7e9 Saving index 3 Using index 3 Code: fc42 Saving index 4 

As you can see, index 2 is skipped because it matches the code in the used set.

+2
source share

All Articles