{"id":12768,"date":"2021-09-30T16:30:38","date_gmt":"2021-09-30T11:00:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=12768"},"modified":"2021-11-22T18:33:34","modified_gmt":"2021-11-22T13:03:34","slug":"python-program-to-generate-random-numbers-except-for-a-particular-number-in-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-generate-random-numbers-except-for-a-particular-number-in-a-list\/","title":{"rendered":"Python Program to Generate Random Numbers except for a Particular Number in a List"},"content":{"rendered":"
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. Then, as previously described, we can use the choice() method to retrieve any random value from the newly generated list.<\/p>\n Given a list, the task is to generate random numbers except for a particular number in a list in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to generate random numbers except for a particular number in a list in Python.<\/p>\n Want to excel in java coding? Practice with these Java Programs examples with output<\/a> and write any kind of easy or difficult programs in the java language<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\n
\nFirst, let’s look at how the random.choice()<\/strong> method works. This method selects a random number from a Python list or tuple and returns it.
\nTo 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.<\/p>\nGiven list = [12, 42, 48, 19, 24, 29, 23, 11, 19, 5, 7, 31, 39, 45, 47, 49]\r\nGiven element =19<\/pre>\n
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 ]<\/pre>\n
Given list = [1, 8, 19, 11, 647, 19, 98, 64, 57, 811, 83] \r\nGiven element=8<\/pre>\n
The Random Element in the given list [1, 8, 19, 11, 647, 19, 98, 64, 57, 811, 83] is [ 19 ]<\/pre>\n
Python Program to Generate Random Numbers except for a particular number in a list<\/h2>\n
\n
Method #1: Using List Comprehension (Static Input)<\/h3>\n
\n
# Import the random module using the import statement.\r\nimport random\r\n# Give the list as static input and store it in a variable.\r\ngvnlst = [12, 42, 48, 19, 24, 29, 23, 11, 19, 5, 7, 31, 39, 45, 47, 49]\r\n# Give the number as static input and store it in a variable.\r\nnumbr = 24\r\n# Using List Comprehension create a new list that does not contain the given number.\r\nnwlist = [elmnt for elmnt in gvnlst if elmnt != numbr]\r\n# Using the random. choice() function generates some random element in the given list.\r\nrndm_numbrr = random.choice(nwlist)\r\n# Print the random element in the given list.\r\nprint('The Random Element in the given list', gvnlst, 'is [', rndm_numbrr, ']'\r\n<\/pre>\n
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 ]<\/pre>\n
Method #2: Using List Comprehension (User Input)<\/h3>\n
\n
# Import the random module using the import statement.\r\nimport random\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvnlst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the number as user input using int(input()) and store it in a variable.\r\nnumbr = int(input('Enter some random Number = '))\r\n# Using List Comprehension create a new list that does not contain the given number.\r\nnwlist = [elmnt for elmnt in gvnlst if elmnt != numbr]\r\n# Using the random. choice() function generates some random element in the given list.\r\nrndm_numbrr = random.choice(nwlist)\r\n# Print the random element in the given list.\r\nprint('The Random Element in the given list', gvnlst, 'is [', rndm_numbrr, ']')\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 1 8 19 11 647 19 98 64 57 811 83\r\nEnter some random Number = 11\r\nThe Random Element in the given list [1, 8, 19, 11, 647, 19, 98, 64, 57, 811, 83] is [ 19 ]<\/pre>\n
\n