In this article, we will learn how to create random numbers in Python except for a specific number. To achieve the required result, we will combine the random.choice() function with the list comprehension technique. See what we can come up with.
First, let’s look at how the random.choice() method works. This method selects a random number from a Python list or tuple and returns it.
To generate a random number from a given list that is not equal to a certain value, we first use the list comprehension method to get a list of elements that are not identical to the provided particular value that must be avoided while generating the random number from the list.
Then, as previously described, we can use the choice() method to retrieve any random value from the newly generated list.
Given a list, the task is to generate random numbers except for a particular number in a list in Python.
Examples:
Example1:
Input:
Given list = [12, 42, 48, 19, 24, 29, 23, 11, 19, 5, 7, 31, 39, 45, 47, 49] Given element =19
Output:
The Random Element in the given list [12, 42, 48, 19, 24, 29, 23, 11, 19, 5, 7, 31, 39, 45, 47, 49] is [ 7 ]
Example2:
Input:
Given list = [1, 8, 19, 11, 647, 19, 98, 64, 57, 811, 83] Given element=8
Output:
The Random Element in the given list [1, 8, 19, 11, 647, 19, 98, 64, 57, 811, 83] is [ 19 ]
Python Program to Generate Random Numbers except for a particular number in a list
Below are the ways to generate random numbers except for a particular number in a list in Python.
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language
Method #1: Using List Comprehension (Static Input)
Approach:
- Import the random module using the import statement.
- Give the list as static input and store it in a variable.
- Give the number as static input and store it in a variable.
- Using List Comprehension create a new list that does not contain the given number.
- Using the random. choice() function generates some random element in the given list.
- Print the random element in the given list.
- The Exit of the Program.
Below is the implementation:
# Import the random module using the import statement. import random # Give the list as static input and store it in a variable. gvnlst = [12, 42, 48, 19, 24, 29, 23, 11, 19, 5, 7, 31, 39, 45, 47, 49] # Give the number as static input and store it in a variable. numbr = 24 # Using List Comprehension create a new list that does not contain the given number. nwlist = [elmnt for elmnt in gvnlst if elmnt != numbr] # Using the random. choice() function generates some random element in the given list. rndm_numbrr = random.choice(nwlist) # Print the random element in the given list. print('The Random Element in the given list', gvnlst, 'is [', rndm_numbrr, ']'
Output:
The Random Element in the given list [12, 42, 48, 19, 24, 29, 23, 11, 19, 5, 7, 31, 39, 45, 47, 49] is [ 7 ]
Method #2: Using List Comprehension (User Input)
Approach:
- Import the random module using the import statement.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give the number as user input using int(input()) and store it in a variable.
- Give the number as static input and store it in a variable.
- Using List Comprehension create a new list that does not contain the given number.
- Using the random. choice() function generates some random element in the given list.
- Print the random element in the given list.
- The Exit of the Program.
Below is the implementation:
# Import the random module using the import statement. import random # Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvnlst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Give the number as user input using int(input()) and store it in a variable. numbr = int(input('Enter some random Number = ')) # Using List Comprehension create a new list that does not contain the given number. nwlist = [elmnt for elmnt in gvnlst if elmnt != numbr] # Using the random. choice() function generates some random element in the given list. rndm_numbrr = random.choice(nwlist) # Print the random element in the given list. print('The Random Element in the given list', gvnlst, 'is [', rndm_numbrr, ']')
Output:
Enter some random List Elements separated by spaces = 1 8 19 11 647 19 98 64 57 811 83 Enter some random Number = 11 The Random Element in the given list [1, 8, 19, 11, 647, 19, 98, 64, 57, 811, 83] is [ 19 ]
Related Programs:
- python program to search the number of times a particular number occurs in a list
- python program to calculate the average of a numbers digits of every number in given list
- python program to find the second largest number in a list
- python program to calculate the average of numbers in a given list
- python program to print largest even and largest odd number in a list
- program to print collatz conjecture for a given number in cpp and python
- python program to print sum of negative numbers positive even numbers and positive odd numbers in a list