Shuffling Lists with random.shuffle()
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 Random Result
Suppose a program needs one random integer from 5 through 10. The function random.randint(5, 10) can produce any integer in that interval. The result may change each time the function is called, so the important question is not which single number appears first, but which numbers are allowed to appear.
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. With bounds 5 and 10, there are six possible integers, and each has an equal 1-in-6 chance on any given call.
The Inclusive Endpoints
The first argument is the low boundary and the second argument is the high boundary. Both boundaries participate in the result. Therefore, random.randint(1, 6) can return 1, 2, 3, 4, 5, or 6. The function does not stop just before the high value.
Calling randint()
import random
roll = random.randint(5, 10)- 1The import statement makes the random module available to the program.
- 2The call supplies two integer arguments: the low boundary 5 and the high boundary 10.
- 3The returned value is assigned to roll.
- Import the random module before using
randint(). - Generate one integer from 5 through 10, including both 5 and 10.
Each printed result is an integer from 5 through 10. The displayed numbers can change from run to run because the function selects a random result. Across the six possible outcomes, each number has an equal 1-in-6 chance on any individual call.
randint() and range()
randint() and range() both use a low value and a high value, but their treatment of the high value differs. random.randint(1, 6) can return 6 because randint() includes its upper endpoint. range(1, 6) excludes 6, so its sequence stops before that upper endpoint.
| Expression | High value included? | Values described by the source |
|---|---|---|
| random.randint(1, 6) | Yes | 1, 2, 3, 4, 5, 6 |
| range(1, 6) | No | 1, 2, 3, 4, 5 |
The upper-bound rule is the key difference between these two functions.
Random Choices in Practice
A random integer is useful when a problem can be represented by numbered choices. For example, a die-roll-style choice can use the bounds 1 and 6. A program that needs a random position can use the first and last valid position numbers as its low and high boundaries. In both cases, the task is to identify the smallest allowed integer and the largest allowed integer, then pass those two integer boundaries to randint().
Representing a Six-Sided Die Roll
Generate one random integer representing a result from 1 through 6.
- 1Identify the boundaries
The smallest allowed result is 1 and the largest allowed result is 6.
- 2Use inclusive bounds
Call
random.randint(1, 6), because both 1 and 6 must be possible results. - 3Interpret the result
The returned integer represents one of the six possible outcomes.
The call random.randint(1, 6) represents a random integer choice from 1 through 6.
Boundary Mistakes
Common mistake Forgetting to import the random module
What it looks likeCalling randint() without first importing random
Why it failsThe source requires the random module to be imported before calling
randint().Do this insteadBegin with import random and call random.randint(low, high).
Common mistake Treating the high value as excluded
What it looks likeAssuming random.randint(1, 6) can return only 1 through 5
Why it failsrandint()includes both its low and high endpoints.Do this insteadInclude the high value when listing the possible results.
Common mistake Passing the bounds in the wrong order
What it looks likeUsing the high boundary first and the low boundary second
Why it failsThe function expects the low boundary followed by the high boundary.
Do this insteadPlace the smaller or low boundary first and the larger or high boundary second.
Common mistake Using non-integer arguments
What it looks likeSupplying bounds that are not integers
Why it failsBoth parameters should be integers.
Do this insteadChoose integer values for both boundaries.
Common mistake Omitting a boundary
What it looks likeTrying to specify only one boundary
Why it failsThe
randint()call is described with both a low and a high parameter.Do this insteadProvide both integer boundaries.
Try It Yourself
Write a Python call that generates one random integer from 10 through 20, including both 10 and 20. Then explain why using range(10, 20) would express a different boundary rule.
Hints
- Import the random module first.
- Place the low boundary before the high boundary.
- Remember that
randint()includes the high boundary, whilerange()excludes its high boundary.
Key Takeaways
- 1
random.randint(low, high)returns a random integer including both low and high. - 2
random.randint(1, 6)can return six possible values: 1, 2, 3, 4, 5, and 6. - 3Unlike
randint(),range()excludes its upper endpoint. - 4Import random before calling
random.randint(), and provide two integer parameters in low-to-high order. - 5Use
randint()when a real-world choice can be represented by an integer interval.
At a glance
randint()includes both boundary values.- The high value is included in
randint()but excluded inrange(). - A correct call requires the random module and two integer boundaries.
- Identifying the smallest and largest allowed values helps model random choices accurately.