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.
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
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 |
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)
| 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 |
Practical Random Integers
Assigning a Random Number
A program needs one random integer from 1 through 6 for a numbered selection.
Import: Import the random module before calling randint().
Set the boundaries: Use 1 as the low value and 6 as the high value because randint() includes both endpoints.
Generate: Call random.randint(1, 6) and store the result.
Use 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()
Forgetting to import the random module
The random module must be imported before randint() is called.
Fix:
Write import random before using random.randint().Assuming the high value is excluded
randint() includes both the low and high endpoints.
Fix:
Include the high value among the possible results.Passing the bounds in the wrong order
The intended low and high values have been reversed.
Fix:
Pass the lower boundary first and the higher boundary second.Using non-integer arguments
The parameters for randint() should be integers.
Fix:
Use integer values for both boundaries.Using an upper list position that is one too high
The high endpoint is included, so 5 can be generated even though the intended positions stop at 4.
Fix:
Set 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
- random.randint(low, high) returns a random integer from low through high.
- Both endpoints are included, so random.randint(5, 10) can return 5, 6, 7, 8, 9, or 10.
- Unlike randint(), range() excludes its upper endpoint.
- Import random first, use integer arguments, and place the lower bound before the higher bound.
- When using a generated number as a list position, make the inclusive upper bound match the final valid position.
Key Takeaways
- 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.