Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to pick a random number not in a list in Python?
Sometimes we need to pick a random number that is not present in a given list. Python provides several approaches to accomplish this task using the random module combined with different data structures and techniques.
Using random.choice() with Loop
The random.choice() function returns a random element from a sequence. We can combine it with a loop to find numbers not in the original list ?
Syntax
random.choice(sequence)
Parameters
sequence ? any sequence like list, tuple, or range
Example
This example creates a new list of numbers not present in the original list, then selects randomly from it ?
import random
# Input list
numbers = [3, 10, 5, 9, 2]
print("Input List =", numbers)
# Get maximum element to define range
max_element = max(numbers)
# Create list of numbers not in original list
available_numbers = []
for i in range(max_element + 5): # Extended range for more options
if i not in numbers:
available_numbers.append(i)
# Select random element from available numbers
random_element = random.choice(available_numbers)
print("Random element not in the list =", random_element)
Input List = [3, 10, 5, 9, 2] Random element not in the list = 8
Using List Comprehension with random.choice()
List comprehension provides a more concise way to create the list of available numbers ?
import random
# Input list
numbers = [3, 10, 5, 9, 2]
print("Input List =", numbers)
# Get maximum element
max_element = max(numbers)
# Create list using comprehension and select random element
random_element = random.choice([i for i in range(max_element + 5) if i not in numbers])
print("Random element not in the list =", random_element)
Input List = [3, 10, 5, 9, 2] Random element not in the list = 7
Using Set Operations
Sets provide efficient operations for finding differences between collections ?
import random
# Input list
numbers = [3, 10, 5, 9, 2]
print("Input List =", numbers)
# Create set of possible numbers and subtract original list
possible_numbers = set(range(min(numbers), max(numbers) + 5))
available_numbers = list(possible_numbers - set(numbers))
# Select random element
random_element = random.choice(available_numbers)
print("Random element not in the list =", random_element)
Input List = [3, 10, 5, 9, 2] Random element not in the list = 4
Using random.randrange() with Loop
This method continuously generates random numbers until finding one not in the list ?
import random
# Input list
numbers = [3, 10, 5, 9, 2]
print("Input List =", numbers)
# Keep generating until we find a number not in the list
while True:
random_element = random.randrange(1, 100)
if random_element not in numbers:
break
print("Random element not in the list =", random_element)
Input List = [3, 10, 5, 9, 2] Random element not in the list = 47
Comparison of Methods
| Method | Efficiency | Best For | Drawback |
|---|---|---|---|
| Loop + choice() | Good | Small ranges | Pre-computes all options |
| List comprehension | Good | Concise code | Memory usage for large ranges |
| Set operations | Very Good | Large datasets | Requires defining range |
| randrange() loop | Variable | Unlimited range | Potentially infinite loop |
Conclusion
Use set operations for efficient processing of large lists. For simple cases with small ranges, list comprehension provides clean, readable code. The randrange() method works well when you need numbers from a very large range.
