Concepts / Shuffling Lists with random.shuffle()

Shuffling Lists with random.shuffle()

random.randint(low, high) generates a random integer within an inclusive range, including both the low and high endpoints.

  • Programming

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?

  • 5, 6, 7, 8, and 9
  • 6, 7, 8, 9, and 10
  • 5, 6, 7, 8, 9, and 10
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.

1low endpoint23456high endpoint
Which integers can be returned, and how do the low and high endpoints participate?

Calling randint()

import random roll = random.randint(5, 10)

python

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.

possible integersvalues in sequencerandint(1, 6)includes 1 through 61, 2, 3, 4, 5, 66 includedrange(1, 6)excludes 61, 2, 3, 4, 56 excluded
Why can randint(1, 6) return 6 while range(1, 6) stops before 6?
ExpressionHigh value included?Values described by the source
random.randint(1, 6)Yes1, 2, 3, 4, 5, 6
range(1, 6)No1, 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.

python

Boundary Mistakes

import randomput low firstuse integersmissing importbeforerandom.randint(low,high)imported module; twointeger boundshigh, lowbeforenon-integer boundsbefore
What changes when the import, bounds, or argument types are incorrect, and what does the corrected call require?
  • 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

EASY

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

  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.
  3. Unlike randint(), range() excludes its upper endpoint.
  4. Import random before calling random.randint(), and provide two integer parameters in low-to-high order.
  5. 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.