Sometimes, while working with Python, we can have a problem in which we need to generate random numbers. This seems quite easy but sometimes we require a slight variation of it. That is, we require to generate random numbers from a list except K. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using choice() + list comprehension
The combination of the above functions can be used to perform this task. In this, we first filter out the numbers except K using list comprehension and then feed that list to choice() for random number generation.
Python3
# Python3 code to demonstrate # Generate random number except K in list # using choice() + list comprehension import random # Initializing list test_list = [ 4 , 7 , 8 , 4 , 6 , 10 ] # printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 4 # Generate random number except K in list # using choice() + list comprehension res = random.choice([ele for ele in test_list if ele ! = K]) # printing result print ( "The random number except K is : " + str (res)) |
The original list is : [4, 7, 8, 4, 6, 10] The random number except K is : 8
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is needed
Method #2 : Using filter() + lambda + choice()
This is yet another way in which this task can be performed. In this, we perform method of creating new list using filter and lambda.
Python3
# Python3 code to demonstrate # Generate random number except K in list # using choice() + filter() + lambda import random # Initializing list test_list = [ 4 , 7 , 8 , 4 , 6 , 10 ] # printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 4 # Generate random number except K in list # using choice() + filter() + lambda res = random.choice( list ( filter ( lambda ele: ele ! = K, test_list))) # printing result print ( "The random number except K is : " + str (res)) |
The original list is : [4, 7, 8, 4, 6, 10] The random number except K is : 8
Method 3: Using random.sample()
This method uses random.sample() function from the random module to select a random element from the list without replacement, which means that the selected element will not be present in the list after being selected.
The code filters out all elements equal to K using list comprehension and then selects one random element from the filtered list using random.sample(). The selected element is then accessed using [0] indexing.
Python3
#Python3 code to demonstrate #Generate random number except K in list #using random.sample() import random #Initializing list test_list = [ 4 , 7 , 8 , 4 , 6 , 10 ] #printing original list print ( "The original list is : " + str (test_list)) #Initializing K K = 4 #Generate random number except K in list #using random.sample() res = random.sample([ele for ele in test_list if ele ! = K], 1 )[ 0 ] #printing result print ( "The random number except K is : " + str (res)) |
The original list is : [4, 7, 8, 4, 6, 10] The random number except K is : 7
Time complexity: O(n) where n is the length of the list
Auxiliary Space: O(n)
Method 4: Using a while loop and random.choice()
- Initialize a variable “res” as K to ensure that the while loop executes at least once.
- Inside the while loop, generate a random number using random.choice() from the given list “test_list”.
- Check if the generated number is not equal to K.
- If the generated number is not equal to K, assign it to the variable “res” and break out of the loop.
- If the generated number is equal to K, continue with the loop until a number different than K is generated.
- Print the result.
Python3
import random # Initializing list test_list = [ 4 , 7 , 8 , 4 , 6 , 10 ] # Printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 4 # Generating random number except K in list # using while loop and random.choice() res = K while res = = K: res = random.choice(test_list) # Printing result print ( "The random number except K is : " + str (res)) |
The original list is : [4, 7, 8, 4, 6, 10] The random number except K is : 10
Time Complexity: O(n) (n is the length of the list)
Auxiliary Space: O(1)
Method 5: the approach used is to sample from a list created by filtering out the value K.
Step-by-step algorithm for the approach:
Step 1: Create a list by filtering out the value K from the original list.
Step 2: Check if the filtered list is not empty.
Step 3: If the filtered list is not empty, select a random element from the filtered list.
Step 4: If the filtered list is empty, raise an exception.
Step 5: Return the selected random element.
Python3
# Initializing list test_list = [ 4 , 7 , 8 , 4 , 6 , 10 ] # printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 4 # Generate random number except K in list without using random module temp_list = [ele for ele in test_list if ele ! = K] if len (temp_list) > 0 : import time t = int ( str (time.time()).replace( '.' ,'')[ - 6 :]) i = t % len (temp_list) res = temp_list[i] else : res = None # printing result if res is not None : print ( "The random number except K is : " + str (res)) else : print ( "No random number except K exists in the list" ) |
The original list is : [4, 7, 8, 4, 6, 10] The random number except K is : 10
Time complexity: The time complexity of this approach is O(n), where n is the length of the original list, as we need to iterate through the list to filter out the value K.
Auxiliary space: The auxiliary space complexity of this approach is O(n), as we need to create a new list to store the filtered values, which can be of size up to n.