Generating Random id's using UUID in Python

UUID stands for Universal Unique Identifier, a Python library that generates 128-bit unique identifiers for random objects. These identifiers are guaranteed to be unique across different systems and time periods.

Advantages of UUID

  • Generates unique random IDs for objects without requiring a central authority
  • Useful for cryptography and hashing applications
  • Perfect for generating unique identifiers for documents, database records, and network addresses
  • No collision risk when generating IDs across distributed systems

Using uuid1() - Time-based UUID

The uuid1() function generates a UUID based on the current timestamp and MAC address ?

import uuid

print("Random id using uuid1() is:", end=" ")
print(uuid.uuid1())
Random id using uuid1() is: 4adeede2-e5d8-11e8-bd27-185e0fd4f8b3

UUID1 Representations and Components

A UUID object provides different representations and contains various components ?

import uuid

id = uuid.uuid1()

# Different representations
print("Different Representations of uuid1():")
print("Representation in bytes:", repr(id.bytes))
print("Representation in int:", id.int)
print("Representation in hex:", id.hex)
print()

# Components
print("Different Components of uuid1():")
print("UUID Version:", id.version)
print("UUID Variant:", id.variant)
print()

# Fields
print("Fields of uuid1():")
print("UUID Fields:", id.fields)
print()

# Time and node components
print("Time and Node Components:")
print("Node component:", id.node)
print("Time component:", id.time)
Different Representations of uuid1():
Representation in bytes: b'\x1a\xd2\xa7F\xe5\xe4\x11\xe8\xbd\x9c\x18^\x0f\xd4\xf8\xb3'
Representation in int: 35653703010223099234452630771665795251
Representation in hex: 1ad2a746e5e411e8bd9c185e0fd4f8b3

Different Components of uuid1():
UUID Version: 1
UUID Variant: specified in RFC 4122

Fields of uuid1():
UUID Fields: (450012998, 58852, 4584, 189, 156, 26792271607987)

Time and Node Components:
Node component: 26792271607987
Time component: 132148464465970000

UUID1 Field Components

  • time_low − First 32 bits of the timestamp
  • time_mid − Next 16 bits of the timestamp
  • time_hi_version − Next 16 bits with version number
  • clock_seq_hi_variant − Next 8 bits with variant bits
  • clock_seq_low − Next 8 bits of clock sequence
  • node − Last 48 bits (usually MAC address)

Using uuid4() - Random UUID

The uuid4() function generates a completely random UUID, which is more secure for most applications ?

import uuid

# Generate random UUID
random_id = uuid.uuid4()
print("Random ID using uuid4():", random_id)

# Generate multiple random UUIDs
print("\nMultiple random UUIDs:")
for i in range(3):
    print(f"UUID {i+1}: {uuid.uuid4()}")
Random ID using uuid4(): 21764219-e3d9-4bd3-a768-0bbc6e376bc0

Multiple random UUIDs:
UUID 1: 8c8f5c42-1d89-4a2b-9f7a-3e4d5f6c7a8b
UUID 2: f47ac10b-58cc-4372-a567-0e02b2c3d479
UUID 3: 6ba7b810-9dad-11d1-80b4-00c04fd430c8

Comparison: UUID1 vs UUID4

Feature UUID1 UUID4
Generation Method Timestamp + MAC address Random numbers
Privacy Less private (exposes MAC) More private
Uniqueness Time-based guarantee Statistical guarantee
Best Use Case When you need time ordering General purpose, security-sensitive

Conclusion

Use uuid4() for most applications requiring random unique identifiers, as it provides better privacy and security. Use uuid1() when you need time-based ordering or want to extract timestamp information from the UUID.

---
Updated on: 2026-03-25T05:03:15+05:30

965 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements