Python Random choices() Method with Examples

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:

random.choices(sequence, weights=None, cum_weights=None, k=1)

Parameters

sequence: This is Required. It could be a list, a tuple, a range of numbers, and so on.

weights: This is Optional. A list where you can weigh the pros and cons of each value.
None is the default.

cum_weights: Optional. A list in which you can weigh the possibility for each value, except that this time the possibility is accumulated.
For instance, the normal weights list [2, 1, 1] is the same as the cum weights list [2, 3, 4].
None is the default.

k: This is Optional. An integer that specifies the length of the returned list.

Examples:

Example1:

Input:

Given List = ["good", "morning", "all"]
Given weights = [5, 1, 1]
Given k value = 10

Output:

['good', 'all', 'good', 'good', 'good', 'morning', 'good', 'good', 'good', 'good']

Explanation:

Return a list containing 10 items.
The list should contain a random selection of values from a given list, with
the possibility of selecting "good" being 5 times greater than the other two

Example2:

Input:

Given List = [1,2,3]
Given weights = [1,1,3]
Given k value = 12

Output:

[2, 1, 3, 3, 3, 2, 3, 3, 3, 2, 2, 3]

Explanation:

Return a list containing 12 items.
The list should contain a random selection of values from a given list, with 
the possibility of selecting "3" being 3 times greater than the other two

Random choices() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import random module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the weights as a list as static input and store it in another variable.
  • Give the k value as static input and store it in another variable.
  • Pass the given list, weights, k value as the arguments to the random.choices() method that returns a list containing the element from the given list that was chosen at random.
  • Store it in another variable.
  • Print a list containing the element from the given list that was chosen at random.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as static input and store it in a variable.
gvn_lst = ["good", "morning", "all"]
# Give the weights as a list as static input and store it in another variable.
gvn_weightss = [5, 1, 1]
# Give the k value as static input and store it in another variable.
gvn_k_val = 10
# Pass the given list, weights, k value as the arguments to the random.choices()
# method that returns a list containing the element from the given list that was
# chosen at random.
# Store it in another variable.
rslt = random.choices(gvn_lst, weights=gvn_weightss, k=gvn_k_val)
# Print a list containing the element from the given list that was chosen at random.
print(rslt)

Output:

['good', 'all', 'good', 'good', 'good', 'morning', 'good', 'good', 'good', 'good']

Explanation:

Return a list containing 10 items.
The list should contain a random selection of values from a given list, with
the possibility of selecting "good" being 5 times greater than the other two

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import random module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the weights as a list as user input using list(),map(),input(),and split() functions and store it in another variable.
  • Give the k value as user input using the int(input()) function and store it in another variable.
  • Pass the given list, weights, k value as the arguments to the random.choices() method that returns a list containing the element from the given list that was chosen at random.
  • Store it in another variable.
  • Print a list containing the element from the given list that was chosen at random.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the weights as a list as user input using list(),map(),input(),and split() 
# functions and store it in another variable.
gvn_weightss =  list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the k value user input using the int(input()) function and store it in another variable.
gvn_k_val = int(input("Enter some random number = "))
# Pass the given list, weights, k value as the arguments to the random.choices()
# method that returns a list containing the element from the given list that was
# chosen at random.
# Store it in another variable.
rslt = random.choices(gvn_lst, weights=gvn_weightss, k=gvn_k_val)
# Print a list containing the element from the given list that was chosen at random.
print(rslt)

Output:

Enter some random List Elements separated by spaces = 1 2 3
Enter some random List Elements separated by spaces = 1 1 3
Enter some random number = 12
[2, 1, 3, 3, 3, 2, 3, 3, 3, 2, 2, 3]

Explanation:

Return a list containing 12 items.
The list should contain a random selection of values from a given list, with 
the possibility of selecting "3" being 3 times greater than the other two