Choosing Random Elements from a List with random.choice()
random.randint(low, high) generates a random integer within an inclusive range, including both the low and high endpoints.
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…
A Number from a Range
Suppose a program needs one random integer from 5 through 10. The function random.randint(5, 10) can produce any integer from that range, including both boundary values. The result can change from one call to another, so the program must be prepared for every allowed outcome.
What do you think happens?
Which values can random.randint(5, 10) return?
Reveal answer
5, 6, 7, 8, 9, and 10
randint() includes both the low endpoint and the high endpoint. There are six possible integers, and each has an equal 1-in-6 chance on any given call.
Tracing the Endpoints
The word inclusive is the key to using randint() correctly. In random.randint(low, high), low is allowed and high is also allowed. Therefore, random.randint(5, 10) has six possible outcomes: 5, 6, 7, 8, 9, and 10. Each outcome has an equal 1-in-6 chance on any one call.
One possible output is:
7
Another call may produce a different value from the same allowed set: 5, 6, 7, 8, 9, or 10.randint() and range()
| Expression | Lower endpoint | Upper endpoint | Possible values in the example |
|---|---|---|---|
| random.randint(5, 10) | Included | Included | 5, 6, 7, 8, 9, 10 |
| range(5, 10) | Included | Excluded | 5, 6, 7, 8, 9 |
randint() includes its high value, while range() excludes its high value.
From a List Index to an Element
A random integer can be used as a position when a program needs to select an element from a list. The important boundary question is whether the integer range matches the list's valid positions. Because randint() includes its upper bound, giving it an upper bound that is one greater than the final valid position can request a position that does not exist.
import random
colors = ["red", "blue", "green", "yellow", "purple"]
position = random.randint(0, 4)
selected_color = colors[position]
print(selected_color)- 1Import the random module before calling
randint(). - 2Create a list with five elements.
- 3Generate an integer from 0 through 4. The upper endpoint 4 is included.
- 4Use the generated position to select one element from the list.
- 5Print the selected element.
| Approach | What is produced first | What happens next |
|---|---|---|
| random.randint() with a list position | A random integer | The integer is used as a list position |
| random.choice() | A selected list element | The element can be used directly |
randint() generates a number; choice() is named for selecting an element directly.
Practical Random Integers
Assigning a Random Number
A program needs one random integer from 1 through 6 for a numbered selection.
- 1Import
Import the random module before calling
randint(). - 2Set the boundaries
Use 1 as the low value and 6 as the high value because
randint()includes both endpoints. - 3Generate
Call
random.randint(1, 6)and store the result. - 4Use the result
The stored integer can be printed or used by later program logic.
The variable contains one integer from 1, 2, 3, 4, 5, or 6.
Mistakes with randint()
number = random.randint(1, 6)The function is called before the random module has been imported.
import random
number = random.randint(1, 6)The module is imported before randint() is called.
Common mistake Forgetting to import the random module
What it looks likenumber = random.randint(1, 6)Why it failsThe random module must be imported before
randint()is called.Do this insteadWrite import random before using random.randint().
Common mistake Assuming the high value is excluded
What it looks likerandom.randint(5, 10) is treated as though 10 cannot occur.Why it failsrandint()includes both the low and high endpoints.Do this insteadInclude the high value among the possible results.
Common mistake Passing the bounds in the wrong order
What it looks likerandom.randint(10, 5)Why it failsThe intended low and high values have been reversed.
Do this insteadPass the lower boundary first and the higher boundary second.
Common mistake Using non-integer arguments
What it looks likerandom.randint(1.5, 6.5)Why it failsThe parameters for
randint()should be integers.Do this insteadUse integer values for both boundaries.
Common mistake Using an upper list position that is one too high
What it looks likeA list with positions 0 through 4 is paired with random.randint(0, 5).
Why it failsThe high endpoint is included, so 5 can be generated even though the intended positions stop at 4.
Do this insteadSet the upper bound to the final valid position.
Practice the Boundaries
Write a Python statement that generates one random integer from 20 through 30, including both 20 and 30. Then identify the mistake in random.randint(0, 5) when it is used to select a position from a five-element list whose positions run from 0 through 4.
Hints
- Import the random module before calling the function.
randint()includes the high value.- For the five-element list example, the highest valid position is 4.
Key Takeaways
- 1
random.randint(low, high)returns a random integer from low through high. - 2Both endpoints are included, so
random.randint(5, 10)can return 5, 6, 7, 8, 9, or 10. - 3Unlike
randint(),range()excludes its upper endpoint. - 4Import random first, use integer arguments, and place the lower bound before the higher bound.
- 5When using a generated number as a list position, make the inclusive upper bound match the final valid position.
At a glance
randint()generates random integers with inclusive lower and upper boundaries.- The upper-bound rule differs from
range(), whose upper endpoint is excluded. - A random integer can serve as a list position only when its bounds match the valid positions.
- Import random, use integer arguments, and check the order and meaning of both bounds.