How to generate 10 digit random number in Python?

Do you want to generate 10 digit unique random number in Python? In this tutorial, I have explained different methods to generate 10 digit random number in Python.

Python random 10 digit number

Here we can see how to generate a random 10 digit number in Python. To get a random number of specified lengths we should set maximum and minimum values.

In this example, I have imported a module random and to get a random number we should generate N and assign minimum – minimum = pow(10, N-1) and maximum – maximum = pow(10, N) – 1 and then return a random number of length as given in the argument.

Example:

import random

def randomnumber(N):
	minimum = pow(10, N-1)
	maximum = pow(10, N) - 1
	return random.randint(minimum, maximum)

print(randomnumber(10))

The below screenshot shows the output:

Python random 10 digit number
Python random 10 digit number

This is how we can generate a random 10 digit number in Python very easily.

Generate 10 digit unique random number in Python

Below are also various methods to generate 10-digit random number in Python programming.

Method 1: Using the random module

Python’s built-in random module can be used to generate random numbers. Let us see various ways to generate a random number in Python.

In this example, I have imported a random module, and random() returns the random floating value.

Example:

import random
random_number = random.random()
print(random_number)
Python generate random number
Python generate random number

This way, we can create a single random number in Python.

READ:  Python Return Function [6 use cases]

Here is another example.

import random

random_number = random.randint(1000000000, 9999999999)
print(random_number)

In the above code snippet, random.randint() function returns a random integer between the two arguments provided, including both endpoints. It will be a 10-digit number like the below screenshot.

generate 10 digit random number in python

Method 2: Using random with String Concatenation

This method involves generating each digit of the 10-digit number randomly and concatenating them as a string in Python. Then, convert that string back to an integer.

import random

random_number = int(''.join([str(random.randint(0, 9)) for _ in range(10)]))
print(random_number)

You can see the output in the below screenshot.

generate 10 digit unique random number in python

Method 3: Using random and Math

You can generate a random floating-point number in the range [0, 1) using random.random() and then scale and shift it to get a 10-digit number in Python.

import random

random_number = int(random.random() * (10**10 - 1) + 10**9)
print(random_number)

This method generates a random float between 0 and 1, multiplies it by (10^10 – 1), and then adds 10^9 to ensure it is 10 digits long. You can see the output below:

generate random 10 digit number python

Method 4: Using secrets module

The secrets module can be used for generating cryptographically secure random numbers. This method is suitable when you require higher security 10 digits random numbers in Python.

import secrets

random_number = secrets.randbelow(9000000000) + 1000000000
print(random_number)

You can see the output like below:

python random 10 digit number
python random 10-digit number

Method 5: Using NumPy Library

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions. We can also use the NumPy library to generate a random 5-digit number in Python or a 10-digit number.

import numpy as np

random_number = np.random.randint(low=10**9, high=10**10 - 1)
print(random_number)

In the above code, np.random.randint() function from the numpy library is used to generate a random integer between 10^9 and 10^10 – 1.

READ:  How to Check N-Digit Armstrong Numbers in Python [3 Examples]

Method 6: Using the uuid module

Though it is generally used to generate unique IDs, you can use the UUID module to generate random numbers as well. By using the below code, we can generate a 10-digit random number.

import uuid

random_number = int(str(uuid.uuid4().int)[:10])
print(random_number)

This method generates a random UUID, converts it to an integer, takes the first 10 digits as a string, and then converts it back to an integer. Check out the output below:

generate random 7 digit number python

Conclusion

I hope you have a complete idea of how to generate a 10-digit random number in Python using various methods as explained above with examples.

You may also like the following tutorials: