So we can generate a unique id with str(uuid.uuid4())
, which is 36 characters long.
Is there another method to generate a unique ID which is shorter in terms of characters?
EDIT:
1. NanoID is Only 108 bytes in Size. Unlike UUID, NanoID is 4.5 times smaller in size and does not have any dependencies.
Universally Unique Identifiers, or UUIDS, are 128 bit numbers, composed of 16 octets and represented as 32 base-16 characters, that can be used to identify information across a computer system.
The randomly generated UUID uses a random number as the source to generate the UUID. In Java, the randomUUID() static method is used to generate a random UUID. The method internally uses SecureRandom class, which provides a cryptographically strong random number generator.
If this is for use as a primary key field in db, consider just using auto-incrementing integer instead.
str(uuid.uuid4())
is 36 chars but it has four useless dashes (-
) in it, and it's limited to 0-9 a-f.
Better uuid4 in 32 chars:
>>> uuid.uuid4().hex
'b327fc1b6a2343e48af311343fc3f5a8'
Or just b64 encode and slice some urandom bytes (up to you to guarantee uniqueness):
>>> base64.b64encode(os.urandom(32))[:8]
b'iR4hZqs9'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With