Tuesday, August 13, 2024

# Creating universal unique IDs

I wanted a way to generate an automatic but unique code or password without it being easily back-traceable and collision-proof. i.e. each time it is executed, it must generate a unique universal ID, aka UUID. Additionally, that code or password must be 8 characters long and must contain a combination of letters and numbers and must contain at least one letter and exactly one letter must be in uppercase.

In this post, I’ll share my solution with explanation and a real-time demonstration of the script that you can run directly from this page (below). But first, some explanation of UUID is in order. Then I’ll also explain the solution and the steps I took to create the solution in Python.

The uuid module in Python contains functions for generating universal unique identifiers (UUIDs). Perfect for this solution. A unique identifier can be used for various purposes, such as naming objects, providing a unique key in a database, etc. UUID is either guaranteed to be different or is, at least, extremely likely to be different from any other UUID generated until 3400 A.D. Due to this nature, UUIDs are ideal to be used anytime a unique identifier is required for many use-cases with 32 digits to choose from.

A UUID is a 128-bit number containing 32 hexadecimal digits, separated by hyphens into five groups, in a 8-4-4-4-12 digit pattern. UUID4 (version 4, which I use in the script below) creates a random UUID that looks something like this: 584ceca0-9c5c-4466-b713-deda3f06935e (the actual numbers will look different each time of course, but the pattern will be the same). While most of the digits will be unique each time, note that the 13th hexadecimal digit is always 4, and the 17th hexadecimal digit is one of: 8, 9, A, or B, indicating the UUID variant.

For my purposes, where I only need 8 digits, I will extract the last 8 unique digits from the generated UUID. It’s not that straightforward as UUID is not a string data type but a special UUID object. Therefore, we’ll need to convert it first so we can apply slicing, indexing, and concatenation as we would on a string object. Also, we have special requirement…one random letter (not a number) from those 8 digits must be in uppercase. In essence, below are the steps:

STEPS:

1. Create a UUID value.
2. Convert the UUID to a string data type so we can use slicing, indexing, and concatenation at will.
3. Get the last n characters (e.g. 8 in this example) from that UUID converted to string.
We could technically choose any other length up to 32 characters, really.
4. Then convert only one of the letters from that string to uppercase using a random index number to choose the character position from the string which we will convert to uppercase.
5. In order to achieve step 4, we need a loop for testing each character in the string to see if that is a letter or number. If it’s a number, uppercasing doesn’t make sense, so we need to continue searching until we find a letter, and when we do, we must convert that character to uppercase and get out of the loop.
6. Finally, we must reconstruct the characters into a string using the uppercased letter into the string of desired length and display it.

Sample outputs from executing the script four times looks like this:

Notice the UUIDs are different, and the random indices it picked and identified as a letter in each position are also different, and that’s exactly the position where the letter was uppercased as shown in the final password/code recommendation (remember: Python indexing is 0-based).

Now, it’s your turn to give it a spin below. Just click the Run button below on the widget and you’ll get the output.

I hope this was educational and interesting. If you need the full source code, feel free to contact me via email trseattle at outlook dot com. All verified donors can request the code free of charge. To become a donor, please click here.

``````
▛Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!
▟``````