Concepts / random.choices() for Multiple Random Selections

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.

  • Programming

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.

positionpositionpositionrandom index is 1List t[1, 2, 3]Index 01Index 122returned elementIndex 23
How does random.choice() use a generated index to return one specific element from a three-element sequence?

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.

python
Output
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]?

  • Only 1 or 3 can be returned
  • Only 2 can be returned
  • 1, 2, or 3 can be returned
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.

selectselect independentlyappend selectionappend selectionSource sequence[1, 2, 3]Selection 1element 2Result list[2, 2]Selection 2element 2
What happens when separate selection operations use the same source sequence, and why can the same element appear more than once?

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.

returnsreturnsrandom.choice()one selectionOne elementreturned directlyrandom.choices()one or more selectionsListselected elements
What is the difference in the number and structure of results returned by random.choice() and random.choices()?
FunctionNumber selectedResult formRepeated elements
random.choice()Exactly oneThe selected element directlyA repeated result can occur across separate calls
random.choices()One or moreA list of selected elementsThe same element can appear multiple times in the list
python

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

EASY

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

  1. random.choice() selects exactly one element from a sequence.
  2. The function generates a random index from 0 through the sequence length minus 1, then returns the element at that position.
  3. Separate calls are independent, so the same element may be selected more than once.
  4. random.choices() selects one or more elements and returns them as a list.
  5. 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.