Understanding the random Module
random.randint(low, high) generates a random integer within an inclusive range, including both the low and high endpoints.
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 |
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.
Identify the lower bound: The smallest permitted rating is 1, so 1 becomes low.
Identify the upper bound: The largest permitted rating is 5, so 5 becomes high. Since randint() includes high, 5 remains a possible result.
Import the module: The random module must be imported before calling randint().
Write 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
Forgetting to import the random module
The call uses random, but the module has not been imported.
Fix:
Add import random before calling random.randint().Treating the high value as excluded
randint() includes its high argument.
Fix:
Use random.randint(5, 10) when the permitted interval includes 10.Passing the bounds in the wrong order
The lower and upper roles have been reversed.
Fix:
Place the smaller integer first and the larger integer second.Using non-integer arguments
randint() requires both parameters to be integers.
Fix:
Supply 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, while range() excludes its upper endpoint.
Key Takeaways
- random.randint(low, high) generates one random integer from an inclusive interval.
- Both low and high can be returned by randint().
- This differs from range(), whose upper endpoint is excluded.
- Import random before calling random.randint(), and pass integer arguments.
- Put the smaller bound first and verify that the high value is intended to be included.
Key Takeaways
- 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.