{"id":25502,"date":"2021-11-23T08:56:08","date_gmt":"2021-11-23T03:26:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=25502"},"modified":"2021-11-23T08:56:08","modified_gmt":"2021-11-23T03:26:08","slug":"python-random-sample-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-random-sample-method-with-examples\/","title":{"rendered":"Python Random sample() Method with Examples"},"content":{"rendered":"
Random sample() Method in Python:<\/strong><\/p>\n The sample() method returns a list containing a randomly selected number of items from a sequence.<\/p>\n Note:<\/strong> Please keep in mind that this method does not alter(change) the original sequence.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n sequence:<\/strong> This is Required. It could be any sequence like a list, a tuple, a range of numbers, and so on.<\/p>\n size:<\/strong> This is Required. The number of items in the returned list.<\/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 Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong> Similarly, you can also try for strings, to get the given number of random characters from a given string.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Random sample() Method in Python: The sample() method returns a list containing a randomly selected number of items from a sequence. Note: Please keep in mind that this method does not alter(change) the original sequence. Syntax: random.sample(sequence, size) Parameters sequence: This is Required. It could be any sequence like a list, a tuple, a range …<\/p>\nrandom.sample(sequence, size)<\/pre>\n
Given list = [4, 7, 9, 0, 1]\r\nGiven size = 3<\/pre>\n
The 3 random items from a given list = [1, 9, 7]<\/pre>\n
Given list = [\"hello\", \"this\", \"is\", \"btechgeeks\"]\r\nGiven size = 2<\/pre>\n
The 2 random items from a given list = ['is', 'btechgeeks']<\/pre>\n
Random sample() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [4, 7, 9, 0, 1]\r\n# Give the size of the list to be returned as static input and store it in\r\n# another variable.\r\ngvn_size = 3\r\n# Pass the given list, size as the arguments to the random.sample() method to\r\n# get the given number(size) of random items from a given list\r\n# Store it in another variable.\r\nrslt = random.sample(gvn_lst, gvn_size)\r\n# Print\u00a0the given number of random items from a given list\r\nprint(\"The\", gvn_size, \"random items from a given list = \", rslt)\r\n<\/pre>\n
The 3 random items from a given list = [1, 9, 7]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import random module using the import keyword.\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\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n\r\n# Give the size of the list to be returned as user input using the int(input())\r\n# function and store it in another variable.\r\ngvn_size = int(input(\"Enter some random number = \"))\r\n# Pass the given list, size as the arguments to the random.sample() method to\r\n# get the given number(size) of random items from a given list\r\n# Store it in another variable.\r\nrslt = random.sample(gvn_lst, gvn_size)\r\n# Print\u00a0the given number of random items from a given list\r\nprint(\"The\", gvn_size, \"random items from a given list = \", rslt)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 23 10 20 30 40 50\r\nEnter some random number = 2\r\nThe 2 random items from a given list = [50, 20]<\/pre>\n","protected":false},"excerpt":{"rendered":"