
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Generating Random id's using UUID in Python
UUID is having the full form Universal Unique Identifier, it is a python library which supports 128 bits ids for generating random objects.
Advantages of UUID
- As discussed, we can use it to generate unique random id for random objects.
- For cryptography and hashing applications, this id can be used.
- For generating random documents and also addresses etc. this id can be used.
Method1
Using uuid1()
Example code
import uuid print ("Random id using uuid1() is : ",end="") print (uuid.uuid1())
Output
Random id using uuid1() is : 4adeede2-e5d8-11e8-bd27-185e0fd4f8b3
Representations of uuid1()
bytes − It returns id in the format of 16 byte string.
int − It returns id in format of 128-bit integer.
hex − As 32 character hexadecimal string, it returns random id.
Components of uuid1()
version − version number of UUID.
variant − It determines the internal layout of UUID.
Fields of uuid1()
time_low −Indicates the first 32 bits of id.
time_mid −Indicates the next 16 bits of id.
time_hi_version − Indicates the next 16 bits of id.
clock_seq_hi_variant − Indicates next 8 bits of id.
clock_seq_low − Indicates next 8 bits of id.
node − Indicates last 48 bits of id.
time − Indicates time component field of id.
clock_seq − Indicates 14 bit sequence number.
Example code
import uuid id = uuid.uuid1() # Representations of uuid1() print ("Different Representations of uuid1() are : ") print ("Representation in byte : ",end="") print (repr(id.bytes)) print ("Representation in int : ",end="") print (id.int) print ("Representation in hex : ",end="") print (id.hex) print("\n") # Components of uuid1() print ("Different Components of uuid1() are : ") print ("UUID Version : ",end="") print (id.version) print ("UUID Variant : ",end="") print (id.variant) print("\n") # Fields of uuid1() print ("Fields of uuid1() are : ") print ("UUID Fields : ",end="") print (id.fields) print("\n") # uuid1() Time Component print ("uuid1() time Component is : ") print ("Time component : ",end="") print (id.node)
Output
Different Representations of uuid1() are : Representation in byte : 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() are : UUID Version : 1 UUID Variant : specified in RFC 4122 Fields of uuid1() are : UUID Fields : (450012998, 58852, 4584, 189, 156, 26792271607987) uuid1() time Component is : Time component : 26792271607987
Method2
Using uuid4()
Example code
import uuid id = uuid.uuid4() # Id generated using uuid4() print ("The id generated using uuid4() : ",end="") print (id)
Output
The id generated using uuid4() : 21764219-e3d9-4bd3-a768-0bbc6e376bc0
- Related Articles
- Generating random Id’s in Python
- Generating random number list in Python
- Generating Random String Using PHP
- Generating random strings until a given string is generated using Python
- Generating random numbers in Java
- Generating random numbers in C#
- Generating Random Numbers in Golang
- Generating a unique random 10 character string using MySQL?
- Generating random hex color in JavaScript
- Generating Random Prime Number in JavaScript
- Generating Random Short Id in Node.js
- Generating random number in a range in C
- Generating random string of specified length in JavaScript
- Generating random string with a specific length in JavaScript
- Generating hash ids using uuid3() and uuid5() in Python
