{"id":16538,"date":"2021-08-12T09:25:10","date_gmt":"2021-08-12T03:55:10","guid":{"rendered":"https:\/\/python-programs.com\/?p=16538"},"modified":"2021-11-22T18:38:33","modified_gmt":"2021-11-22T13:08:33","slug":"python-program-to-get-n-random-items-from-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-get-n-random-items-from-a-list\/","title":{"rendered":"Python Program to Get n Random Items from a List"},"content":{"rendered":"
In the previous article, we have discussed Python Program Divide all Elements of a List by a Number<\/a> As this Random module is one of Python’s predefined modules, its methods return random values.<\/p>\n It selects integers uniformly from a range. For sequences, it has a function to generate a random permutation of a list in-place, as well as a function to generate a random sampling without replacement. Let’s take a look at how to import the Random Module.<\/p>\n The random module in Python is made up of various built-in Methods.<\/p>\n choice():<\/strong>\u00a0 choice() is used to select an item at random from a list, tuple, or other collection.<\/p>\n Because the choice() method returns a single element, we will be using it in looping statements. Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to Get n Random Items from a Given List.<\/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 Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program Divide all Elements of a List by a Number Random Module in python : As this Random module is one of Python’s predefined modules, its methods return random values. It selects integers uniformly from a range. For sequences, it has a function to generate a random …<\/p>\n
\nRandom Module in python :<\/strong><\/p>\n
\nsample(): <\/strong>To meet our needs, we’ll use sample() to select multiple values.<\/p>\nGiven no of random numbers to be generated = 5\r\nGiven list =[1, 2, 3, 2, 2, 1, 4, 5, 6, 8, 9]<\/pre>\n
The given 5 Random numbers are :\r\n2\r\n3\r\n8\r\n6\r\n9<\/pre>\n
Given no of random numbers to be generated = 3\r\nGiven list = [2, 1, 6, 1, 4, 5, 6, 8]<\/pre>\n
The given 3 Random numbers are :\r\n6\r\n5\r\n1<\/pre>\n
Program to Get n Random Items from a List<\/h2>\n
\n
Method #1: Using random.choice() Method (Static input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number as static input and store it in a variable.\r\nrandm_numbrs = 3\r\n# Give the list as static input and store it in another variable.\r\ngvn_lst = [1, 2, 3, 2, 2, 1, 4, 5, 6, 8, 9]\r\nprint(\"The given\", randm_numbrs, \"Random numbers are :\")\r\n# Loop above given 'n' number of times using For loop.\r\nfor itr in range(randm_numbrs):\r\n # Inside the loop, apply random.choice() method for the above given list and\r\n # store it in a variable.\r\n reslt = random.choice(gvn_lst)\r\n # Print the given numbers of random numbers to be generated.\r\n print(reslt)\r\n<\/pre>\n
The given 3 Random numbers are :\r\n5\r\n3\r\n4<\/pre>\n
Method #2: Using random.choice() Method (User input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number as user input using int(input()) and store it in a variable.\r\nrandm_numbrs = int(input(\"Enter some random number = \"))\r\n#Give the list as user input and using list(),map(), int(),input(),and split() functions \r\n#store it in another variable\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\nprint(\"The given\", randm_numbrs, \"Random numbers are :\")\r\n# Loop above given 'n' number of times using For loop.\r\nfor itr in range(randm_numbrs):\r\n # Inside the loop, apply random.choice() method for the above given list and\r\n # store it in a variable.\r\n reslt = random.choice(gvn_lst)\r\n # Print the given numbers of random numbers to be generated.\r\n print(reslt)\r\n<\/pre>\n
Enter some random number = 4\r\nEnter some random List Elements separated by spaces = 1 2 3 4 5 6 7 8 9 \r\nThe given 4 Random numbers are :\r\n2\r\n2\r\n4\r\n8<\/pre>\n
\n