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.
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?
Arrange the sequence: The list contains 1 at index 0, 2 at index 1, and 3 at index 2.
Choose an index: The selection happens at index 1.
Read 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
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)
The 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 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
Using random.choice() without importing random.
The random module must be imported before random.choice() can be used.
Fix:
Add import random before the call.Expecting random.choice() to return an index.
The generated integer is used internally as an index; the function returns the element at that position.
Fix:
Treat selected as one element from items.Assuming repeated calls cannot return the same element.
Each call is independent, so the same element may be selected multiple times by chance.
Fix:
Allow each call to produce any element in the sequence.Confusing random.choice() with random.choices().
random.choice() selects one element directly, whereas random.choices() returns selected elements as a list and allows sampling with replacement.
Fix:
Use 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.
Map positions: The value north is at index 0, south is at index 1, and east is at index 2.
Read the selected position: Index 2 refers to east.
Trace 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
- random.choice() selects exactly one element from a sequence.
- It generates a random integer that acts as an index and returns the element at that position.
- Each call is independent, so repeated calls can return the same element.
- Import the random module before using random.choice().
- random.choices() differs by returning multiple selected elements in a list and allowing sampling with replacement.
Key Takeaways
- 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.