Python random.choices() method (original) (raw)
Last Updated : 05 Dec, 2024
The **choices() method returns multiple random elements from the list with replacement. Unlike random.choice(), which selects a single item, random.choices() allows us to select multiple items making it particularly useful for tasks like sampling from a population or generating random data.
**Example:
Python `
import random
a = ["geeks", "for", "python"]
print(random.choices(a, weights = [10, 1, 1], k = 5))
`
Output
['geeks', 'geeks', 'geeks', 'geeks', 'geeks']
Explanation:
- The code uses random.choices() to select 5 items from the list a = ["geeks", "for", "python"].
- **Weights: The item "geeks" has a weight of 10, making it 10 times more likely to be selected than "for" and "python", which each have a weight of 1.
- **k = 5: Specifies that 5 items will be selected, with replacement (meaning duplicates are possible).
**Note: Every time output will be different as the system returns random elements.
Let's take a closer look at random.choices() method:
Table of Content
- Syntax of random.choices()
- Example of random.choices() method:
- Practical Applications of random.choices() method:
Syntax of random.choices()
random.choices(population, weights=None, cum_weights=None, k=1)
Parameters
- **population: The sequence (list, tuple, string, etc.) from which random selections are made.
- **weights: (Optional) A sequence of weights corresponding to each element in the population. Elements with higher weights are more likely to be selected.
- **cum_weights: (Optional) A sequence of cumulative weights. If provided, this is an alternative to using the weights parameter.
- **k: The number of items to return. By default, it returns one item.
**Example of random.choices() method:
Python `
import random
Example 1: Simple Random Selection
a = ['apple', 'banana', 'cherry', 'date'] res = random.choices(a, k=3) print(res)
Example 2: Random Selection with Weights
w = [10, 20, 5, 1] res = random.choices(a, weights=w, k=3) print(res)
Example 3: Random Selection with Cumulative Weights
cw = [10, 30, 35, 36] # Cumulative sum of weights res = random.choices(a, cum_weights=cw, k=3) print(res)
Example 4: Selecting Random Characters from a String
ch = "abcdefghijklmnopqrstuvwxyz" res = random.choices(ch, k=5) print(res)
`
Output
['cherry', 'apple', 'apple'] ['cherry', 'banana', 'banana'] ['cherry', 'cherry', 'banana'] ['c', 'd', 'm', 'z', 's']
**Explanation:
- **Example 1: Selects 3 random elements from the items list.
- **Example 2: Selects 3 elements from items with specified weights, making some items more likely to be selected.
- **Example 3: Selects 3 elements with cumulative weights, ensuring that selection probabilities are proportionally distributed.
- **Example 4: Selects 5 random characters from the alphabet string, demonstrating the flexibility of random.choices() with strings.
Practical Applications of random.choices() method:
It allows for sampling with replacement, meaning the same item can be selected multiple times. Common practical applications include:
- **Lottery or Prize Draws: Randomly selecting winners from a pool of entries, where some participants may have more chances based on certain conditions.
- **Simulating Weighted Outcomes: For example, simulating biased coin flips or biased random events (e.g., selecting between different items in a game where certain items are rarer).
- **Random Sampling: Selecting items from a list for tasks like A/B testing, survey sampling, or testing combinations.