Concepts / random.choices() for Multiple Random Selections

random.choices() for Multiple Random Selections

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.

  • Programming
Interactive lab

Try it: random.seed and the Mersenne Twister

Python's random module is a deterministic generator: random.seed(n) sets the Mersenne Twister's internal state, and every random(), randint(), choice() and shuffle() consumes 32-bit outputs from it in order — so the same seed replays the same sequence and a different seed gives a different one.

How it works

  1. random.seed(n) splits |n| into 32-bit words and runs MT19937's init_by_array, filling the 624-word state.
  2. Each 32-bit output takes the next state word and tempers it; after 624 words the whole state is regenerated (the twist).
  3. random() joins two outputs: with , , a float in [0.0, 1.0).
  4. randint(a, b) = a + _randbelow(b − a + 1); choice(seq) = seq[_randbelow(len(seq))]; _randbelow(n) keeps the top n.bit_length() bits and rejects values ≥ n.
  5. shuffle walks i from the end down to 1 and swaps x[i] with x[_randbelow(i + 1)].
  6. Replaying the program with a second seed shows where the sequences agree or first diverge.

Default run (11 steps): A program of 5 random-module calls, run after random.seed(42). … Replay with random.seed(42): every result is identical — same seed, same state, same sequence.

Simplified: Seeds are non-negative integers below (one key word), at most 8 calls, randint bounds within ±1000, and lists of up to 8 simple items. Only mt[0..3] and mt[623] of the 624-word state vector are shown (abbreviated). Everything else follows CPython 3.12's random module exactly.

Educational simulation

Loading the simulation…

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().

Key point

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.

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

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?

  1. 1
    Identify the valid positions

    There are three elements, so the possible index positions are 0, 1, and 2.

  2. 2
    Use the generated index

    The generated index is 1, so the function accesses the element at position 1.

  3. 3
    Return the element

    The element at index 1 is 2, so random.choice() returns 2.

Result

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.

Source sequence[1, 2, 3]Selection 1element 2Result list[2, 2]Selection 2element 2selectselect independentlyappend selectionappend selection
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.

random.choice()one selectionOne elementreturned directlyrandom.choices()one or more selectionsListselected elementsreturnsreturns
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

The essential distinction is both how many elements are selected and how the result is returned.

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

  • Common mistake

    Forgetting to import the random module

    What it looks like

    Calling random.choice(items) without first writing import random.

    Why it fails

    The source requires the random module to be imported before random.choice() is used.

    Do this instead

    Write import random before calling random.choice() or random.choices().

  • Common mistake

    Expecting random.choice() to return a list

    What it looks like

    Treating the result of random.choice(items) as though it contains multiple selected elements.

    Why it fails

    random.choice() returns one selected element directly.

    Do this instead

    Use random.choices() when the required result is a list of one or more selected elements.

  • Common mistake

    Assuming a later call must choose a different element

    What it looks like

    Expecting two calls on the same sequence to avoid returning the same element twice.

    Why it fails

    Each call is independent, so the same element can be selected multiple times by chance.

    Do this instead

    Treat every call as a fresh selection from the sequence.

  • Common mistake

    Ignoring the final s in random.choices()

    What it looks like

    Using random.choice() when the task requires multiple selected elements in a list.

    Why it fails

    The two functions differ in both the number of selections and the structure of the result.

    Do this instead

    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.
Worked example

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?

  1. 1
    Map index 2

    The element at index 2 is 3.

  2. 2
    Return the first result

    The first call returns 3.

  3. 3
    Trace the next call

    The second call generates a fresh index, so it may return 1, 2, or 3.

Result

The first result is 3, and the next independent call may return any element in the sequence.

Key Takeaways

  1. 1
    random.choice() selects exactly one element from a sequence.
  2. 2
    The function generates a random index from 0 through the sequence length minus 1, then returns the element at that position.
  3. 3
    Separate calls are independent, so the same element may be selected more than once.
  4. 4
    random.choices() selects one or more elements and returns them as a list.
  5. 5
    Import the random module before using these functions.

At a glance

  • 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.

More concepts starting with R