My company has asked me to develop a 4-byte random hexadecimal generator for some security hardware we have, however I am new to this sort of thing. (To clarify, each code consists of 4 hexadecimal digits).
We currently have a list of 124 hexadecimals that are in use and I need to be able to not repeat those hexadecimals as well as ones that will be generated with this program. I have developed a GUI and a generator, I just need help making sure it never repeats.
This is what I have so far:
# 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()
Your question asks about a 4-byte random hexadecimal generator, but in the comments you clarify that you only want 4 hex digits, which means there are only
2**16
combinations. That makes the problem rather easy: we just create a list of all 65,536 combinations and shuffle it, and then we can simply iterate over that shuffled list. To save a little time & RAM I create a list of integers & just convert the integers to hex strings as needed.The fact that you've got a list of 124 codes that are already in use adds a little bit of complexity, but we can handle that by putting those codes into a set; we can easily test the generated codes to see if they're in the
used
set.Obviously, we want to be able to run the program multiple times, so we save the index number of the shuffled list into a text file. This is simpler and more efficient than storing each number that we generate into the
used
set.We also need the randomization to be consistent, so we need to supply a seed to the random number generator.
Here's some Python 3 code. It can be adapted to run on Python 2 (by changing
FileNotFoundError
toIOError
), but you cannot switch versions between runs because the sequence of random numbers generated by Python 2 will not be the same as that generated by Python 3output from 3 runs
As you can see, index 2 gets skipped, that's because it corresponds to a code in the
used
set.