You are currently viewing Python Random choices() Function

random.choices() function is one of the functions of the built-in random module in Python and is used to select one or more elements from a given sequence(population) randomly with replacement. A random module is used to generate the random numbers in Python.

Advertisements

In this article, I will explain random.choices() function and using its syntax, parameters, and usage how we can return one or more elements from a sequence such as list, tuple, set, and string.

1. Quick Examples of random.choices()

Following are quick examples of random.choices() function.


# Below are the quick examples

# Initialize the list
my_list = ["Spark", "by", "examples"]

# Example 1: Get list of random element
rand_list = random.choices(my_list)
print("Random number:", rand_list)

# Example 2: Get list of multiple random elements
rand_list = random.choices(my_list, k = 2)

# Example 3: Get list of random elements by specifing weights
rand_list = random.choices(my_list, weights = [2, 3, 1], k = 5)
print("Random list:", rand_list)

# Example 4: Get list of single character using choices()
# Initialize string
my_str = "Python"
print("String:", my_str)
print("Random character:", random.choices(my_str))

2. random.choices() Function

Python random.choices() function is part of a random module that returns a list of randomly selected elements from a given sequence with replacement. Where the length of the list(the number of selections) is specified by using the ‘k’ parameter of the choices() function.

2.1 Syntax of random.choices()

Following is the syntax of random.choices() function.


# syntax of random.choices()
random.choices(population, weights=None, cum_weights=None, k=1)

2.2 Parameters

Following are the parameters of choices() function.

  • population: It is a sequence, using this you want to randomly select elements. It can be a list, tuple, string, or any other iterable object.
  • weights (optional): It is a list or tuple of weights that correspond to each element in the population. the probability of each element selection depends upon the weight of an element. If weights are not specified, the probability of each element selection is equal.
  • cum_weights (optional): It is a list or tuple of cumulative weights that correspond to each element in the population. The cumulative weight of an element is the sum of the weights of all preceding elements. If weights and cum_weights are both specified, cum_weights will be used.
  • k (optional): It is the number of selections. By default, it is set to 1.

2.3 Return value

It returns a list of elements/elements randomly from a given population.

3. Usage of choices()

Let’s take a list and pass it into the choices() function, to get the list of the single random element from the given list. If you do not specify the number of selections, it takes the default value ‘k’ as ‘1’.


# Get list of random element
# Initialize the list
my_list = ["Spark", "by", "examples"]
print("List:", my_list)
rand_list = random.choices(my_list)
print("Random number:", rand_list)

Yields below output.

Python random choices

4. Get a List of Multiple Random Elements using choices()

When you want to get a list of multiple elements randomly with replacement(repetition), you can set the k parameter with the number of selections you want to get from the list. For example,


# Get list of multiple random elements
# Initialize the list
my_list = ["Spark", "by", "examples"]
print("List:", my_list)
rand_list = random.choices(my_list, k = 2)
print("Random number:", rand_list)

Yields below output.

Python random choices

You can also use the weights parameter to specify a probability distribution for the elements in the population. For example,


# Get list of random elements by specifing weights
rand_list = random.choices(my_list, weights = [2, 3, 1], k = 5)
print("Random list:", rand_list)

# Output:
# List: ['Spark', 'by', 'examples']
# Random list: ['Spark', 'by', 'by', 'by', 'by']

As you can see from the above, the spark element has twice the chance to select as a random element than the examples element, and the by element has three times chance to select as a random element than the examples element.

5. Get the Random Character using Python choices()

You can also use the choices() function over the string and get the list of random character/characters from the string. Let’s pass the string into the choices() function to get the list of a single character from the string.


# Get list of single character using choices()
# Initialize string
my_str = "Python"
print("String:", my_str)
print("Random character:", random.choices(my_str))

# Output:
# String: Python
# Random character: ['y']

6. Conclusion

In this article, I have explained random.choices() function of Python and using its syntax, parameters, and usage how we can generate the selected elements randomly from the given sequence with examples.

Happy learning!!

References