{"id":25520,"date":"2021-11-19T09:12:50","date_gmt":"2021-11-19T03:42:50","guid":{"rendered":"https:\/\/python-programs.com\/?p=25520"},"modified":"2021-11-19T09:12:50","modified_gmt":"2021-11-19T03:42:50","slug":"python-random-choices-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-random-choices-method-with-examples\/","title":{"rendered":"Python Random choices() Method with Examples"},"content":{"rendered":"
Random choices() Method in Python:<\/strong><\/p>\n The choices() method returns a list containing the element from the specified sequence that was chosen at random.<\/p>\n You can use the weights or cum weights parameters to weigh the likelihood of each result.<\/p>\n The sequence could be a string, a range, a list, a tuple, or anything else.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n sequence:<\/strong> This is Required. It could be a list, a tuple, a range of numbers, and so on.<\/p>\n weights:<\/strong> This is Optional. A list where you can weigh the pros and cons of each value. cum_weights:<\/strong> Optional. A list in which you can weigh the possibility for each value, except that this time the possibility is accumulated. k:<\/strong> This is Optional. An integer that specifies the length of the returned list.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Random choices() Method in Python: The choices() method returns a list containing the element from the specified sequence that was chosen at random. You can use the weights or cum weights parameters to weigh the likelihood of each result. The sequence could be a string, a range, a list, a tuple, or anything else. Syntax: …<\/p>\nrandom.choices(sequence, weights=None, cum_weights=None, k=1)<\/pre>\n
\nNone is the default.<\/p>\n
\nFor instance, the normal weights list [2, 1, 1] is the same as the cum weights list [2, 3, 4].
\nNone is the default.<\/p>\nGiven List = [\"good\", \"morning\", \"all\"]\r\nGiven weights = [5, 1, 1]\r\nGiven k value = 10<\/pre>\n
['good', 'all', 'good', 'good', 'good', 'morning', 'good', 'good', 'good', 'good']<\/pre>\n
Return a list containing 10 items.\r\nThe list should contain a random selection of values from a given list, with\r\nthe possibility of selecting \"good\" being 5 times greater than the other two<\/pre>\n
Given List = [1,2,3]\r\nGiven weights = [1,1,3]\r\nGiven k value = 12<\/pre>\n
[2, 1, 3, 3, 3, 2, 3, 3, 3, 2, 2, 3]<\/pre>\n
Return a list containing 12 items.\r\nThe list should contain a random selection of values from a given list, with \r\nthe possibility of selecting \"3\" being 3 times greater than the other two<\/pre>\n
Random choices() 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 = [\"good\", \"morning\", \"all\"]\r\n# Give the weights as a list as static input and store it in another variable.\r\ngvn_weightss = [5, 1, 1]\r\n# Give the k value as static input and store it in another variable.\r\ngvn_k_val = 10\r\n# Pass the given list, weights, k value as the arguments to the random.choices()\r\n# method that returns a list containing the element from the given list that was\r\n# chosen at random.\r\n# Store it in another variable.\r\nrslt = random.choices(gvn_lst, weights=gvn_weightss, k=gvn_k_val)\r\n# Print a list containing the element from the given list that was chosen at random.\r\nprint(rslt)\r\n<\/pre>\n
['good', 'all', 'good', 'good', 'good', 'morning', 'good', 'good', 'good', 'good']<\/pre>\n
Return a list containing 10 items.\r\nThe list should contain a random selection of values from a given list, with\r\nthe possibility of selecting \"good\" being 5 times greater than the other two<\/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# Give the weights as a list as user input using list(),map(),input(),and split() \r\n# functions and store it in another variable.\r\ngvn_weightss = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the k value user input using the int(input()) function and store it in another variable.\r\ngvn_k_val = int(input(\"Enter some random number = \"))\r\n# Pass the given list, weights, k value as the arguments to the random.choices()\r\n# method that returns a list containing the element from the given list that was\r\n# chosen at random.\r\n# Store it in another variable.\r\nrslt = random.choices(gvn_lst, weights=gvn_weightss, k=gvn_k_val)\r\n# Print a list containing the element from the given list that was chosen at random.\r\nprint(rslt)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 1 2 3\r\nEnter some random List Elements separated by spaces = 1 1 3\r\nEnter some random number = 12\r\n[2, 1, 3, 3, 3, 2, 3, 3, 3, 2, 2, 3]<\/pre>\n
Return a list containing 12 items.\r\nThe list should contain a random selection of values from a given list, with \r\nthe possibility of selecting \"3\" being 3 times greater than the other two<\/pre>\n","protected":false},"excerpt":{"rendered":"