random.randint() for Random Integers
random.choice() selects one element at random from a sequence by generating a random index and returning the element at that position.
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 Element
Suppose a sequence contains several possible values and your program should select one of them. random.choice() performs that selection. It returns exactly one element from the sequence, but the selected element can differ from one call to the next.
From Index to Value
random.choice() selects an element by using a random index. For a sequence of length three, the possible indices are 0, 1, and 2. The function generates one of those index values and returns the element stored at that position. The index is the position used for selection; the returned result is the element found there.
Following One Selection
A list contains 1, 2, and 3. What value is returned if random.choice() selects index 1?
- 1Arrange the sequence
The list contains 1 at index 0, 2 at index 1, and 3 at index 2.
- 2Choose an index
The selection happens at index 1.
- 3Read the element
The element at index 1 is 2, so
random.choice()returns 2.
The returned value is 2.
Tracing Repeated Calls
What do you think happens?
The list t contains 1, 2, and 3. If the first call to random.choice(t) returns 2 and the second returns 3, what can happen on a third call?
Reveal answer
It can return 1, 2, or 3.
Each call independently generates a fresh random index. A third call may return any element, including an element returned by an earlier call.
import random
t = [1, 2, 3]
first = random.choice(t)
second = random.choice(t)
print(first)
print(second)- 1The random module is imported so
random.choice()is available. - 2The name t refers to a three-element list.
- 3The first call selects one element from t.
- 4The second call performs a new independent selection from t.
- 5The displayed values are one possible result, not a guaranteed result for every run.
- The list has three possible positions: 0, 1, and 2.
- The first call can select index 1 and return 2.
- The second call can select index 2 and return 3.
One possible output:
2
3The important tracing habit is to separate the random position from the value stored at that position. First identify the sequence and its indices. Then identify the selected index. Finally read the element at that index. A later call starts the process again with a fresh random index.
Sequences and Selection
random.choice() receives a sequence and returns one element from it. A list is a common example. The selected position determines the returned content, so changing the sequence's contents changes the possible results, while the selection mechanism remains the same.
choice() and choices()
| Function | How many elements | Return shape | Replacement behavior |
|---|---|---|---|
| random.choice() | Exactly one | The selected element directly | A later independent call can select the same element |
| random.choices() | One or more | A list of selected elements | Allows sampling with replacement, so an element can be selected multiple times |
The key differences between random.choice() and random.choices().
The function name's final s matters. random.choice() returns one selected element directly. random.choices() returns selected elements in a list and allows the same element to appear more than once through sampling with replacement.
Random Integer and Element
Inside random.choice(), the generated random integer is used as an index. For a sequence with three elements, the possible index values are 0, 1, and 2. The function does not return that index as its normal result; it uses the index to return the element at that position. This is the essential difference between an index and the selected sequence element.
Mistakes Beginners Make
Common mistake Using random.choice() without importing random.
What it looks likeselected = random.choice(items)Why it failsThe random module must be imported before
random.choice()can be used.Do this insteadAdd import random before the call.
Common mistake Expecting random.choice() to return an index.
What it looks likeitems = ["A", "B", "C"] selected = random.choice(items)Why it failsThe generated integer is used internally as an index; the function returns the element at that position.
Do this insteadTreat selected as one element from items.
Common mistake Assuming repeated calls cannot return the same element.
What it looks likefirst = random.choice(items) second = random.choice(items)Why it failsEach call is independent, so the same element may be selected multiple times by chance.
Do this insteadAllow each call to produce any element in the sequence.
Common mistake Confusing random.choice() with random.choices().
What it looks likerandom.choice(items) and random.choices(items)Why it failsrandom.choice()selects one element directly, whereasrandom.choices()returns selected elements as a list and allows sampling with replacement.Do this insteadUse random.choice() for one element and random.choices() when multiple selected elements in a list are required.
Practice the Trace
Consider the sequence values = ["north", "south", "east"]. Trace a call to random.choice(values) when the generated index is 2. State the returned element. Then explain why a second call could return the same element or a different one.
Hints
- Write the indices beside the values, starting with index 0.
- Use the element at index 2 as the result.
- Remember that each call generates a fresh random index.
Practice Solution
For values = ["north", "south", "east"], random.choice(values) uses index 2.
- 1Map positions
The value north is at index 0, south is at index 1, and east is at index 2.
- 2Read the selected position
Index 2 refers to east.
- 3Trace another call
A later call independently generates a new random index, so it can select east again or select another element.
The first call returns east. A later call may return east, north, or south.
Key Takeaways
- 1
random.choice()selects exactly one element from a sequence. - 2It generates a random integer that acts as an index and returns the element at that position.
- 3Each call is independent, so repeated calls can return the same element.
- 4Import the random module before using
random.choice(). - 5
random.choices()differs by returning multiple selected elements in a list and allowing sampling with replacement.
At a glance
random.choice()returns one randomly selected element from a sequence.- The selection process uses a random index between 0 and the sequence length minus 1.
- The selected index and the returned element are different ideas: the index identifies a position, while the element is the value at that position.
- Independent calls may return the same element.
random.choices()returns a list of one or more selections instead of one direct element.