656 words
3 minutes
ZUID: Generating Even More Unique Identifiers

The story begins with a need: I was looking for a unique identifier algorithm that could produce more unique identifiers than UUIDv4—the most widely used one. But before diving in, let’s clarify what a UUID actually is.

What is a UUID?#

A Universally Unique Identifier (UUID) is a 128-bit number, composed of 16 octets and typically represented as 32 hexadecimal characters. It’s used to uniquely identify information across computer systems. The specification was originally created by Microsoft and later standardized by both the IETF and ITU.

In this article, I’ll focus specifically on UUID version 4 (UUIDv4), which is purely random generated (unlike other versions that are based on timestamps or MAC addresses).

It’s worth noting that generating a duplicate UUID is extremely unlikely.

A sample of 3.26 × 10¹⁶ UUIDs has a 99.99% chance of not containing any duplicates. At a rate of one UUID per second, generating that many would take over a billion years. — towardsdatascience

A Closer Look#

As mentioned earlier, a UUID is a 128-bit number represented in hexadecimal format. That means you can generate your own UUID by randomly changing even a single bit.

For example, if you imagine the smallest possible UUID as '0x00000000000000000000000000000000', where all bits are zero, the next one would be '0x00000000000000000000000000000001', and it goes on. Because of this vast space, collisions are almost impossible. According to Wikipedia, you’d need to generate about 2.71 quintillion UUIDs before expecting a single collision—that’s a truly massive number.

But then a question popped into my head: Can we make an identifier that’s even more unique—or at least appears more diverse—than a UUID? Let me explain :)

Tweaking the Output#

While thinking about it, I realized we could modify the final section of the UUID generation process—the part that converts the 128-bit number into a hexadecimal string. This step gives us a 32-character-long identifier. If we take that final string and convert or encode it into a different format of the same length, we essentially get a new kind of identifier.

Alphabet Character Mapping#

My first idea was to remap the hexadecimal characters. In hex, the digits 10 through 15 are represented by the letters 'a' through 'f'. So, what if we replaced 'abcdef' with a different set of six characters?

I chose to map 'abcdef' to 'uvwxyz'—the last six letters of the alphabet.

For example, the UUID '14be1be3-4818-46da-bb60-dc2b1d89c8ff' becomes '14yv1yv3-4818-46wz-yy60-wx2y1w89x8uu'.

The map_to argument in the function can be any set of 6 characters, which opens up room for customization and variety.

Random Uppercasing#

Another trick to increase uniqueness is to randomly change the case of alphabetic characters. This adds visual variety without altering the base information.

The full source code of ZUID—my custom identifier generator—is shown below:

# a library that generates zuid.
import random
from uuid import uuid4
class zuid:
zuid = 0x000000000000000000000000000000000000
def __init__(self, zuid=None):
if (zuid != None and zuid != self.zuid):
self.zuid = bytes(zuid, "utf-8")
def should_upper(self):
return random.randint(0, 1024) % 2 == 0
def map(self, data, map_to):
board = f"abcdef{map_to}"
mapped = ""
for i in data:
if (i in board):
mapped += board[len(board) - 1 - board.find(i)]
else:
mapped += i
return mapped
def generate_zuid1(self):
digits = "0123456789"
uuidchars = "abcdef"
zuidcharacters = "uvwxyz"
characters = zuidcharacters.upper()
zuid_digits = digits + characters
uuid = str(uuid4())
template = "xxxxxxxx-xxxx-zxxx-xxxx-xxxxxxxxxxxx"
zuid = ""
for i in range(len(template)):
if (template[i] == 'z'):
zuid += 'z'
elif (template[i] == '-'):
zuid += '-'
else:
uuidchar = uuid[i]
if (uuidchar in digits):
zuid += uuidchar
else:
char = self.map(uuid[i], zuidcharacters)
zuid += char.upper() if self.should_upper() else char
return zuid
def __str__(self) -> str:
if (self.zuid != None):
return self.zuid.decode("utf-8")
else:
raise Exception("ZUID is not initialized")
def __repr__(self):
if (self.zuid != None):
return '%s(%r)' % (self.__class__.__name__, str(self))
else:
raise Exception("ZUID is not initialized")
def zuid1():
return zuid(zuid().generate_zuid1())

Conclusion#

ZUID’s uniqueness builds on the reliability of UUIDv4, but adds layers of transformation—character mapping, casing, and potential encodings—to create identifiers that are visually distinct and even more customizable.

You can also roll your own variant by generating random bits or experimenting with different encodings.