random.choices() for Multiple Random Selections
random.choice() selects one element at random from a sequence by generating a random index and returning the element at that position.
One Call, One Result
Random selection becomes easier to understand when you separate two related operations. random.choice() selects exactly one element from a sequence and returns that element directly. random.choices(), with an s, selects one or more elements and returns the selections as a list. This article focuses on the mechanism behind random.choice() and then uses that mechanism to clarify multiple selection with random.choices().
The result shape is the first distinction to remember: random.choice() returns one selected element, while random.choices() returns a list containing its selected elements.
Following the Index
When random.choice() receives a sequence, it internally generates a random integer from 0 through the sequence length minus 1. That integer is used as an index, or position, in the sequence. The function then returns the element stored at that position. For a three-element list, the possible index positions are 0, 1, and 2.
Tracing One Selection
A sequence contains 1 at index 0, 2 at index 1, and 3 at index 2. What is returned if random.choice() generates index 1?
Identify the valid positions: There are three elements, so the possible index positions are 0, 1, and 2.
Use the generated index: The generated index is 1, so the function accesses the element at position 1.
Return the element: The element at index 1 is 2, so random.choice() returns 2.
The selected element is 2.
Repeating the Selection
Every call to random.choice() generates a fresh random index. The calls do not have to move through the sequence in order, and a later call does not avoid an element selected earlier. Because each call is independent, the same element may be selected more than once, or different elements may be selected on successive calls.
The two variables each contain one element from t. One possible pair is 2 followed by 3. Another possible pair could contain the same element twice, because the calls are independent.What do you think happens?
After random.choice(t) returns 2 once, what can happen when random.choice(t) is called again for t = [1, 2, 3]?
Reveal answer
Answer: 1, 2, or 3 can be returned.
Each call generates a fresh random index. The earlier result does not remove 2 from the sequence or restrict the next call.
Choosing the Right Function
The names differ by only one letter, but the return values differ in an important way. random.choice() selects exactly one element and returns that element directly. random.choices() selects one or more elements and returns those selections in a list. The plural function also supports sampling with replacement, which means the same element can occur multiple times in the returned list.
| Function | Number selected | Result form | Repeated elements |
|---|---|---|---|
| random.choice() | Exactly one | The selected element directly | A repeated result can occur across separate calls |
| random.choices() | One or more | A list of selected elements | The same element can appear multiple times in the list |
In this example, one_item represents one element selected from items. several_items represents a list of selected elements. Since random.choices() samples with replacement, a list produced by that function may contain the same element more than once.
Mistakes Beginners Make
Forgetting to import the random module
The source requires the random module to be imported before random.choice() is used.
Fix:
Write import random before calling random.choice() or random.choices().Expecting random.choice() to return a list
random.choice() returns one selected element directly.
Fix:
Use random.choices() when the required result is a list of one or more selected elements.Assuming a later call must choose a different element
Each call is independent, so the same element can be selected multiple times by chance.
Fix:
Treat every call as a fresh selection from the sequence.Ignoring the final s in random.choices()
The two functions differ in both the number of selections and the structure of the result.
Fix:
Check whether the task needs one direct element or a list of selections.
Practice the Trace
Suppose t = [1, 2, 3]. Trace a call to random.choice(t) when the generated index is 2. Then explain what could happen during a second independent call.
Hints
- List the valid index positions for the three elements.
- Find the element stored at index 2.
- For the second call, remember that a fresh random index is generated.
Checking the Reasoning
For t = [1, 2, 3], a call to random.choice(t) generates index 2. What is returned, and what may a second call return?
Map index 2: The element at index 2 is 3.
Return the first result: The first call returns 3.
Trace the next call: The second call generates a fresh index, so it may return 1, 2, or 3.
The first result is 3, and the next independent call may return any element in the sequence.
Key Takeaways
- random.choice() selects exactly one element from a sequence.
- The function generates a random index from 0 through the sequence length minus 1, then returns the element at that position.
- Separate calls are independent, so the same element may be selected more than once.
- random.choices() selects one or more elements and returns them as a list.
- Import the random module before using these functions.
Key Takeaways
- random.choice() maps one randomly generated index to one element in a sequence.
- For a sequence of length three, the possible indices are 0, 1, and 2.
- Each call is independent, so repeated calls can return the same element.
- random.choices() differs because it returns a list containing one or more selections.
- The plural function permits repeated elements through sampling with replacement.