Concepts / Understanding the random Module

Understanding the random Module

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

  • Programming

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().

python

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.

51-in-6 chance61-in-6 chance71-in-6 chance81-in-6 chance91-in-6 chance101-in-6 chance
Which individual integer values can random.randint(5, 10) return?
Output
5, 6, 7, 8, 9, or 10

Understanding 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.

includesincludesincludesexcludesrandint(5, 10)low and high included5included5included10includedrange(5, 10)high excluded10excluded
Which boundary values are included when the lower value is 5 and the upper value is 10?
FunctionLower endpointUpper endpoint
random.randint(low, high)IncludedIncluded
range(low, high)The source material identifies the upper-endpoint differenceExcluded

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.

python
Output
1, 2, 3, 4, or 5
choose low and highgeneratesRating 1 through 5smallest 1, largest 5randint(1, 5)both endpoints includedRandom rating1, 2, 3, 4, or 5
How does the requirement for a rating from 1 through 5 become a randint() call and then a program value?

Correcting 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.

identify boundscorrect orderlow greater than highretry with corrected orderInteger intervalchoose low and highlow before highcheck argument rolesrandint(low, high)integer argumentsCorrect bound ordersmaller first
How can you correct a randint() call when the lower bound is greater than the upper bound?

Practice the Boundary Rule

EASY

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.
MEDIUM

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

  1. random.randint(low, high) generates one random integer from an inclusive interval.
  2. Both low and high can be returned by randint().
  3. This differs from range(), whose upper endpoint is excluded.
  4. Import random before calling random.randint(), and pass integer arguments.
  5. 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.