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.
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
- random.seed(n) splits |n| into 32-bit words and runs MT19937's init_by_array, filling the 624-word state.
- Each 32-bit output takes the next state word and tempers it; after 624 words the whole state is regenerated (the twist).
- random() joins two outputs: with , , a float in [0.0, 1.0).
- 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.
- shuffle walks i from the end down to 1 and swaps x[i] with x[_randbelow(i + 1)].
- 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.
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().
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?
- 1Identify the valid positions
There are three elements, so the possible index positions are 0, 1, and 2.
- 2Use the generated index
The generated index is 1, so the function accesses the element at position 1.
- 3Return 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
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 |
The essential distinction is both how many elements are selected and how the result is returned.
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 likeCalling random.choice(items) without first writing import random.
Why it failsThe source requires the random module to be imported before
random.choice()is used.Do this insteadWrite import random before calling random.choice() or random.choices().
Common mistake Expecting random.choice() to return a list
What it looks likeTreating the result of random.choice(items) as though it contains multiple selected elements.
Why it failsrandom.choice()returns one selected element directly.Do this insteadUse 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 likeExpecting two calls on the same sequence to avoid returning the same element twice.
Why it failsEach call is independent, so the same element can be selected multiple times by chance.
Do this insteadTreat every call as a fresh selection from the sequence.
Common mistake Ignoring the final s in random.choices()
What it looks likeUsing random.choice() when the task requires multiple selected elements in a list.
Why it failsThe two functions differ in both the number of selections and the structure of the result.
Do this insteadCheck 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?
- 1Map index 2
The element at index 2 is 3.
- 2Return the first result
The first call returns 3.
- 3Trace 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. - 2The function generates a random index from 0 through the sequence length minus 1, then returns the element at that position.
- 3Separate 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. - 5Import 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.