How to Get a sorted list of random integers with unique elements using Python?

Generating random numbers is one of the most popular techniques in programming, statistics, machine learning models, etc. Creating a sorted list of random integers with unique elements is a common task. In this article, we will explore different approaches to get a sorted list of random integers with unique elements using Python.

Using sample() Function from Random Module

The random.sample() method generates random samples of k elements from a given population without replacement, ensuring unique elements.

Syntax

random.sample(population, k)
sorted(iterable, key=None, reverse=False)

The sample() function takes a population (iterable) and returns k unique elements as a list. The sorted() function sorts any iterable in ascending order by default.

Example

import random

def generate_sorted_random_integers(start_range, end_range, num_elements):
    random_list = sorted(random.sample(range(start_range, end_range + 1), num_elements))
    return random_list

start_range = 1
end_range = 100
num_elements = 10
random_list = generate_sorted_random_integers(start_range, end_range, num_elements)
print(f"The sorted list of random integers is: {random_list}")
The sorted list of random integers is: [6, 18, 19, 55, 63, 75, 88, 91, 92, 94]

Using NumPy Module

NumPy provides numpy.random.choice() with replace=False to ensure unique elements.

Syntax

numpy.random.choice(array, size=None, replace=True, p=None)

Example

import numpy as np

def generate_sorted_random_integers(start_range, end_range, num_elements):
    random_list = np.sort(np.random.choice(range(start_range, end_range + 1), 
                                         size=num_elements, replace=False))
    return random_list

start_range = 10
end_range = 100
num_elements = 10
random_list = generate_sorted_random_integers(start_range, end_range, num_elements)
print(f"The sorted list of random integers is: {random_list}")
The sorted list of random integers is: [23 27 61 72 74 79 80 90 96 99]

Using Set with List Comprehension

For guaranteed unique elements when using random.randint(), we can use a set to eliminate duplicates ?

Example

import random

def generate_sorted_random_integers(start_range, end_range, num_elements):
    unique_numbers = set()
    while len(unique_numbers) < num_elements:
        unique_numbers.add(random.randint(start_range, end_range))
    return sorted(list(unique_numbers))

start_range = 10
end_range = 50
num_elements = 10
random_list = generate_sorted_random_integers(start_range, end_range, num_elements)
print(f"The sorted list of random integers is: {random_list}")
The sorted list of random integers is: [12, 13, 15, 16, 25, 28, 29, 32, 47, 49]

Using Lambda Function

Lambda functions provide a concise way to create anonymous functions for simple operations ?

Example

import random

generate_sorted_random_integers = lambda start, end, count: sorted(random.sample(range(start, end + 1), count))

start_range = 1
end_range = 100
num_elements = 10
random_list = generate_sorted_random_integers(start_range, end_range, num_elements)
print(f"The sorted list of random integers is: {random_list}")
The sorted list of random integers is: [7, 14, 32, 46, 55, 68, 79, 84, 88, 90]

Using Pandas Library

Pandas can be used to generate multiple sorted lists of random integers using the apply() method ?

Example

import pandas as pd
import random

df = pd.DataFrame({
    'start_range': [1, 1, 1],
    'end_range': [100, 100, 100],
    'num_elements': [10, 10, 10]
})

def generate_sorted_random_integers(row):
    random_list = random.sample(range(row['start_range'], row['end_range'] + 1), row['num_elements'])
    return sorted(random_list)

random_lists = df.apply(generate_sorted_random_integers, axis=1).tolist()
print(f"Multiple sorted lists of random integers: {random_lists}")
Multiple sorted lists of random integers: [[11, 28, 31, 32, 35, 58, 73, 82, 88, 96], [17, 26, 42, 45, 47, 55, 89, 97, 99, 100], [26, 32, 66, 73, 74, 76, 85, 87, 93, 100]]

Comparison

Method Guarantees Uniqueness Best For
random.sample() Yes Simple and efficient
numpy.choice() Yes (with replace=False) Large datasets
Set with while loop Yes When range is limited
Lambda function Yes (using sample) One-liner solutions

Conclusion

The random.sample() method is the most straightforward approach for generating sorted lists of unique random integers. Use NumPy for performance with large datasets, or sets when dealing with small ranges where duplicates are likely.

Updated on: 2026-03-27T10:24:48+05:30

494 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements