Hi Guys,
My objective is to select 5 unique random numbers from a range of numbers inside a list, prior to storing them inside a final list. These 5 random numbers shall not be numbers from another list. However, my code below doesn't have the desired outcome that I want to achieve. Please advise me based on my code below. Thank you in advance.
My objective is to select 5 unique random numbers from a range of numbers inside a list, prior to storing them inside a final list. These 5 random numbers shall not be numbers from another list. However, my code below doesn't have the desired outcome that I want to achieve. Please advise me based on my code below. Thank you in advance.
import random # The "outcast_list" list contains numbers to be filtered off. outcast_list = [1, 2, 3, 4, 5] # The "whole_list" list contains numbers that the generated number shall randomly choose from. whole_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] selection_list = [] status = False # Create a loop until the "status" turns True (i.e. when the list of randomized numbers has 5 numbers inside it.). while status is False: # Generate one random from the "whole_list" one at a time. i = random.sample(whole_list, 1) # Create the following 3 conditions before adding the number into the "selection_list" list. if (i not in outcast_list) and (i not in selection_list) and (status is False): selection_list.append(i) # Break out of the "while" loop when the "selection_list" has 5 numbers inside it. if len(selection_list) == 5: status = True break print(selection_list) # Execution outcome: [[10], [3], [8], [4], [7]] # Desired outcome: [6, 7, 8, 9, 10]