Understanding the random Module
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 Range with Two Included Ends
Suppose a program needs one random integer between 5 and 10. The key question is whether 5 and 10 are allowed results. With random.randint(5, 10), both boundary values are included, so the possible results are 5, 6, 7, 8, 9, and 10. This inclusive behavior is the central idea behind random.randint().
Tracing the Possible Results
For random.randint(5, 10), the lower bound is 5 and the upper bound is 10. Because both bounds are included, there are six possible integer outcomes. The source material states that each outcome has an equal 1-in-6 chance on any given call.
5, 6, 7, 8, 9, or 10Understanding the Two Bounds
The call random.randint(low, high) uses two integer parameters. low identifies the lower boundary of the permitted interval, and high identifies the upper boundary. Unlike Python's range() function, randint() includes the upper endpoint. Therefore, random.randint(5, 10) can produce 10, while the upper endpoint in range(5, 10) is excluded.
| Function | Lower endpoint | Upper endpoint |
|---|---|---|
| random.randint(low, high) | Included | Included |
| range(low, high) | The source material identifies the upper-endpoint difference | Excluded |
The important distinction is the treatment of the high value.
When choosing bounds for randint(), decide first whether the upper value should be a possible result. If it should, use randint() with that value as high. Do not automatically transfer the upper-endpoint rule from range(), because range() excludes its upper endpoint.
Turning a Requirement into a Call
A practical problem can be translated into randint() by identifying the smallest allowed integer and the largest allowed integer. For example, if a program needs a random whole-number rating from 1 through 5, the lower bound is 1 and the upper bound is 5. Because randint() includes both endpoints, the call is random.randint(1, 5). This example is generated to apply the inclusive-range rule to a practical requirement.
Generating a Random Rating
Generate one random integer rating from 1 through 5, including both 1 and 5.
- 1Identify the lower bound
The smallest permitted rating is 1, so 1 becomes low.
- 2Identify the upper bound
The largest permitted rating is 5, so 5 becomes high. Since
randint()includes high, 5 remains a possible result. - 3Import the module
The random module must be imported before calling
randint(). - 4Write the call
Use
random.randint(1, 5)to generate the integer.
The program generates one random integer from 1 through 5.
1, 2, 3, 4, or 5Correcting Invalid Calls
Common mistake Forgetting to import the random module
What it looks likenumber = random.randint(5, 10)Why it failsThe call uses random, but the module has not been imported.
Do this insteadAdd import random before calling random.randint().
Common mistake Treating the high value as excluded
What it looks likeUsing random.randint(5, 9) when 10 is intended to be a possible result
Why it failsrandint()includes its high argument.Do this insteadUse random.randint(5, 10) when the permitted interval includes 10.
Common mistake Passing the bounds in the wrong order
What it looks likerandom.randint(10, 5)Why it failsThe lower and upper roles have been reversed.
Do this insteadPlace the smaller integer first and the larger integer second.
Common mistake Using non-integer arguments
What it looks likerandom.randint(1.5, 5.5)Why it failsrandint()requires both parameters to be integers.Do this insteadSupply integer values for both low and high.
Practice the Boundary Rule
Write a Python statement that generates a random integer from 20 through 25, including both 20 and 25. Then explain why the second argument must be 25 rather than 24.
Hints
- Import the random module first.
- Use the smallest permitted integer as low.
randint()includes the high argument.
A class chooses a random integer from 3 through 8. Identify the low and high arguments, list the possible endpoint values, and state how this differs from range(3, 8).
Hints
- The lower endpoint is 3.
- The upper endpoint is 8.
randint()includes the upper endpoint, whilerange()excludes its upper endpoint.
Key Takeaways
- 1
random.randint(low, high)generates one random integer from an inclusive interval. - 2Both low and high can be returned by
randint(). - 3This differs from
range(), whose upper endpoint is excluded. - 4Import random before calling
random.randint(), and pass integer arguments. - 5Put the smaller bound first and verify that the high value is intended to be included.
At a glance
random.randint(low, high)includes both boundary values.- For
random.randint(5, 10), the six possible outcomes are 5, 6, 7, 8, 9, and 10. - Unlike
randint(),range()excludes its upper endpoint. - Correct use requires importing random, passing integers, and placing the lower bound first.