Shuffling Lists with random.shuffle()
random.randint(low, high) generates a random integer within an inclusive range, including both the low and high endpoints.
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
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)
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 |
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.
Identify the boundaries: The smallest allowed result is 1 and the largest allowed result is 6.
Use inclusive bounds: Call random.randint(1, 6), because both 1 and 6 must be possible results.
Interpret 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
Forgetting to import the random module
The source requires the random module to be imported before calling randint().
Fix:
Begin with import random and call random.randint(low, high).Treating the high value as excluded
randint() includes both its low and high endpoints.
Fix:
Include the high value when listing the possible results.Passing the bounds in the wrong order
The function expects the low boundary followed by the high boundary.
Fix:
Place the smaller or low boundary first and the larger or high boundary second.Using non-integer arguments
Both parameters should be integers.
Fix:
Choose integer values for both boundaries.Omitting a boundary
The randint() call is described with both a low and a high parameter.
Fix:
Provide 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, while range() excludes its high boundary.
Key Takeaways
- random.randint(low, high) returns a random integer including both low and high.
- random.randint(1, 6) can return six possible values: 1, 2, 3, 4, 5, and 6.
- Unlike randint(), range() excludes its upper endpoint.
- Import random before calling random.randint(), and provide two integer parameters in low-to-high order.
- Use randint() when a real-world choice can be represented by an integer interval.
Key Takeaways
- randint() includes both boundary values.
- The high value is included in randint() but excluded in range().
- A correct call requires the random module and two integer boundaries.
- Identifying the smallest and largest allowed values helps model random choices accurately.