Python - Generate a Random Number of Specific Length


Python - Generate a Random Number of Specific Length

We know that the randint() function generates a random number. In this tutorial, we are going to simulate a specific scenario where the random number should be of a specified length.

Using the randint() function from Python's random module, you can generate a random number of a specific length by setting its minimum and maximum values.

Following is the snippet that demonstrates the usage of randint() function.

import random
randomnumber = random.randint(minimum, maximum)

To generate a random number of a given length, all we have to do is set the minimum and maximum values.

Consider that the length of the random number that we should generate is N. Then, the minimum value would be pow(10, N-1) and the maximum value would be pow(10, N)-1.


Examples

1. Generate a random number of specific length

In the following example, we will define a function that generates and returns a random number of a specific length, passed as an argument to it.

Python Program

import random

def randN(N):
	min = pow(10, N-1)
	max = pow(10, N) - 1
	return random.randint(min, max)

# Generate random numbers of different lengths
print(randN(5))
print(randN(7))
print(randN(4))
print(randN(8))

Explanation:

  1. The function randN(N) generates a random number of length N.
  2. We compute the minimum value as pow(10, N-1) and the maximum value as pow(10, N) - 1.
  3. The randint() function is used to generate a random number between minimum and maximum.
  4. Examples of numbers generated include a 5-digit, 7-digit, 4-digit, and 8-digit number.

Output

94839
1176870
4216
63706007

2. Generate Random Number of Length 1 (Single Digit)

In this example, we'll generate a single-digit random number.

Python Program

import random

def randN(N):
	min = pow(10, N-1)
	max = pow(10, N) - 1
	return random.randint(min, max)

# Generate a single-digit random number
print(randN(1))

Explanation:

  1. For N=1, the random number will always be between 0 and 9 (single-digit number).
  2. This demonstrates how the function can handle edge cases like a 1-digit random number.

Output

7

3. Generate Random Number of Length 12

In this example, we generate a random number of 12 digits.

Python Program

import random

def randN(N):
	min = pow(10, N-1)
	max = pow(10, N) - 1
	return random.randint(min, max)

# Generate a 12-digit random number
print(randN(12))

Explanation:

  1. For N=12, the random number will be a 12-digit number.
  2. This shows that the function is capable of generating numbers with varying lengths, including large numbers.

Output

497893249587

4. Generate Random Number with Length of 0 (Edge Case)

In this example, we test what happens if we try to generate a random number with length 0.

Python Program

import random

def randN(N):
	if N <= 0:
		raise ValueError('Length must be a positive integer')
	min = pow(10, N-1)
	max = pow(10, N) - 1
	return random.randint(min, max)

# Test with length 0 (will raise an error)
try:
	print(randN(0))
except ValueError as e:
	print(e)

Explanation:

  1. For N=0, the function now raises an error, preventing invalid input.
  2. We added a check to ensure that N is positive, preventing mathematical errors.
  3. This highlights the importance of input validation and error handling.

Expected Output

Length must be a positive integer

Summary

In this tutorial of Python Examples, we learned how to generate a random number of specific length using the randint() method. We explored examples from generating numbers of standard lengths to edge cases like a single digit and invalid length.


Python Libraries