A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/generating-random-ids-using-uuid-python/ below:

Generating Random id's using UUID in Python

Generating Random id's using UUID in Python

Last Updated : 04 Apr, 2025

We had discussed the ways to generate unique id's in Python without using any python inbuilt library in Generating random Id’s in Python

In this article we would be using inbuilt functions to generate them. A UUID is 128 bits in total, but uuid4() provides 122 bits of randomness due to version and variant bits. It provides the uniqueness as it generates ids on the basis of time, Computer hardware (MAC etc.).

Note: uuid4() uses Python's random module, which may not be cryptographically secure in all implementations. For security-sensitive applications, consider using secrets or os.urandom.

Advantages of UUID :

Method 1 : Using uuid1()

uuid1() is defined in UUID library and helps to generate the random id using

MAC address and time component Python
import uuid

print ("The random id using uuid1() is : ",end="")
print (uuid.uuid1())

Output :

The random id using uuid1() is : 67460e74-02e3-11e8-b443-00163e990bdb
Representations of uuid1() Components of uuid1() Fields of uuid1() Python
import uuid

id = uuid.uuid1()

# Representations of uuid1()
print ("The Representations of uuid1() are : ")
print ("byte Representation : ",end="")
print (repr(id.bytes))

print ("int Representation : ",end="")
print (id.int)

print ("hex Representation : ",end="")
print (id.hex)

print("\n")

# Components of uuid1()
print ("The Components of uuid1() are : ")
print ("Version  : ",end="")
print (id.version)

print ("Variant : ",end="")
print (id.variant)

print("\n")

# Fields of uuid1()
print ("The Fields of uuid1() are : ")
print ("Fields  : ",end="")
print (id.fields)

print("\n")

# Time Component of uuid1()
print ("The time Component of uuid1() is : ")
print ("Time component  : ",end="")
print (id.node)

Output :

The Representations of uuid1() are : 
byte Representation : b'k\x10\xa1n\x02\xe7\x11\xe8\xaeY\x00\x16>\x99\x0b\xdb'
int Representation : 142313746482664936587190810281013480411
hex Representation : 6b10a16e02e711e8ae5900163e990bdb

The Components of uuid1() are :


Version : 1
Variant : specified in RFC 4122

The Fields of uuid1() are :


Fields : (1796252014, 743, 4584, 174, 89, 95539497947)

The time Component of uuid1() is :


Time component : 95539497947
Drawback :

This way includes the used of MAC address of computer, and hence can compromise the privacy, even though it provides uniquenes.

Method 2 : Using uuid4()

Unlike uuid1(), uuid4() does not rely on the MAC address or timestamps, making it more suitable for privacy-sensitive applications. The function below generates a random UUID with 122 bits of entropy, making collisions unlikely but not impossible and doesn't compromises with privacy.

Python
import uuid

id = uuid.uuid4()

# Id generated using uuid4()
print ("The id generated using uuid4() : ",end="")
print (id)

Output :

The id generated using uuid4() : fbd204a7-318e-4dd3-86e0-e6d524fc3f98


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4