How to Generate a 4-digit Random Unique Number Using Python?

Hello Friends Today, through this tutorial, I will tell you how to generate a 4-digit random unique OTP number using Python Language Program?

You can generate a 4-digit random OTP (One-Time Password) in Python using the `random` module. Here’s an example:

import random

def generate_otp():
# Generate a random 4-digit OTP
otp = random.randint(1000, 9999)
return otp

# Example usage
otp_number = generate_otp()
print("Generated OTP:", otp_number)

In this example, the `generate_otp` function uses the `random.randint` function to generate a random integer between 1000 and 9999 (inclusive), which ensures that the OTP is a 4-digit number. The generated OTP is then printed. You can use the `generate_otp` function in your code wherever you need to generate a random 4-digit OTP.