How to generate non-repeating random numbers in Python?

Generating non-repeating random numbers is a common requirement in Python applications. This article demonstrates four different approaches: using randint() with a loop, random.sample() with lists or ranges, and random.choices().

Using randint() with Loop and Checking

This method generates random numbers one by one and checks if they already exist in the result list ?

import random

# Create empty list for storing unique random numbers
random_numbers = []

# Generate 10 unique random numbers between 1 and 100
while len(random_numbers) < 10:
    num = random.randint(1, 100)
    if num not in random_numbers:
        random_numbers.append(num)

print("Non-repeating random numbers:")
print(random_numbers)
Non-repeating random numbers:
[34, 78, 12, 56, 89, 45, 23, 67, 91, 38]

Note: This approach becomes inefficient when the range is small compared to the number of unique values needed.

Using random.sample() with a List

The random.sample() method selects unique elements from a sequence without replacement ?

Syntax

random.sample(sequence, k)

Parameters

  • sequence − Any sequence like list, tuple, or range
  • k − Number of unique elements to select
import random

# Sample from an existing list with duplicates
input_list = [1, 2, 3, 1, 4, 3, 5, 7, 9, 8, 2, 3]

# Remove duplicates using set, then convert back to list
unique_list = list(set(input_list))
print("Unique elements:", unique_list)

# Select 4 random non-repeating numbers
result = random.sample(unique_list, 4)
print("4 non-repeating random numbers:", result)
Unique elements: [1, 2, 3, 4, 5, 7, 8, 9]
4 non-repeating random numbers: [8, 3, 1, 7]

Using random.sample() with Range

You can directly sample from a range of numbers without creating an intermediate list ?

import random

# Generate 5 unique random numbers from range 1 to 50
numbers = random.sample(range(1, 51), 5)
print("5 non-repeating random numbers from 1-50:")
print(numbers)

# Generate 8 unique numbers from range 0 to 99
numbers = random.sample(range(100), 8)
print("8 non-repeating random numbers from 0-99:")
print(numbers)
5 non-repeating random numbers from 1-50:
[23, 7, 41, 15, 38]
8 non-repeating random numbers from 0-99:
[67, 12, 84, 3, 56, 91, 28, 45]

Using random.choices() with Unique Check

Important: random.choices() allows replacement by default, so it can generate duplicates. To get unique values, combine it with a set ?

import random

# Generate unique numbers using choices() with set conversion
numbers_set = set()
while len(numbers_set) < 6:
    numbers_set.update(random.choices(range(1, 21), k=6))

unique_numbers = list(numbers_set)[:6]
print("6 non-repeating random numbers using choices():")
print(unique_numbers)
6 non-repeating random numbers using choices():
[4, 12, 7, 15, 18, 9]

Comparison of Methods

Method Efficiency Best Use Case Guarantees Uniqueness
randint() + loop Low (for small ranges) Small number of values needed Yes
random.sample() High Most scenarios Yes
random.choices() Medium When you need weighted selection No (needs additional logic)

Conclusion

For most cases, use random.sample() as it's efficient and guarantees unique values. Use the loop method only for simple cases with small datasets. The random.choices() method is better suited for weighted selection rather than generating unique numbers.

Updated on: 2026-03-24T20:18:41+05:30

48K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements