 # How To Generate N Unique Random Numbers Within A Range In Python

To generate N unique random numbers within a range in Python, you can use the random.sample(), random.shuffle(), or random.randint() functions. Let’s learn how to use them.

## Generate N Unique Random Numbers Within A Range In Python

### Using the random.sample() function

You can use the random.sample() function to generate N unique random numbers within a range in Python. But note that, you have to assign N as less or equal to the number of elements in a range. If not, you will get an Error.

Look at the example below.

import random

def random_n_unique_numbers(start, end, n):
# Create a range.
my_range = range(start, end)

# Generate N Unique Random Numbers Within A Range with the random.sample() function.
result = random.sample(my_range, n)
print(result)

# Generate 7 unique random numbers between 1 and 10.
random_n_unique_numbers(1, 10, 7)

# Generate 7 unique random numbers between 1 and 100.
random_n_unique_numbers(1, 100, 7)

Output

[5, 2, 1, 6, 7, 9, 4]
[21, 24, 57, 65, 49, 32, 64]

### Using the random.shuffle() function

Besides the random.sample() function, you can use the random.shuffle() function to generate N unique random numbers within a range. Unlike the random.sample() function, we will random a list of a range first. Then, we will get the first n elements of the new list.

Look at the example below.

import random

def random_n_unique_numbers(start, end, n):
# Create a range.
my_range = range(start, end)

# Generate N Unique Random Numbers Within A Range with the random.sample() function.
result = random.sample(my_range, n)
print(result)

# Generate 7 unique random numbers between 1 and 10.
random_n_unique_numbers(1, 10, 7)

# Generate 7 unique random numbers between 1 and 100.
random_n_unique_numbers(1, 100, 7)

Output

[9, 2, 3, 8, 6, 1, 4]
[81, 26, 44, 62, 97, 29, 14]

### Using the random.randint() function

In addition, you can use the random.randint() function combined with the set data structure to generate N unique random numbers within a range. We will use the random.randint() function until the size of the set is N.

Look at the example below.

import random

def random_n_unique_numbers(start, end, n):
# Create a range.
my_range = range(start, end)

# Generate N Unique Random Numbers Within A Range with the random.sample() function.
result = random.sample(my_range, n)
print(result)

# Generate 7 unique random numbers between 1 and 10.
random_n_unique_numbers(1, 10, 7)

# Generate 7 unique random numbers between 1 and 100.
random_n_unique_numbers(1, 100, 7)

Output

[1, 4, 5, 6, 7, 9, 10]
[65, 35, 38, 75, 76, 13, 95]

Besides, you can learn how to generate random numbers in the range excluding some numbers here.

## Summary

We have shown you how to generate N unique random numbers within a range in Python in 3 ways. Personally, you should use the random.sample() or random.shuffle() functions because they have higher performance than the random.randint() function in this case. Leave your comment below if you have any questions. Thanks!