{"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>
\nRandom Module in python :<\/strong><\/p>\n

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.
\nsample(): <\/strong>To meet our needs, we’ll use sample() to select multiple values.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given no of random numbers to be generated = 5\r\nGiven list =[1, 2, 3, 2, 2, 1, 4, 5, 6, 8, 9]<\/pre>\n

Output:<\/strong><\/p>\n

The given 5 Random numbers are :\r\n2\r\n3\r\n8\r\n6\r\n9<\/pre>\n

Example 2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given no of random numbers to be generated = 3\r\nGiven list = [2, 1, 6, 1, 4, 5, 6, 8]<\/pre>\n

Output:<\/strong><\/p>\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

Below are the ways to Get n Random Items from a Given List.<\/p>\n