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
Interactive lab

Try it: random.seed and the Mersenne Twister

Python's random module is a deterministic generator: random.seed(n) sets the Mersenne Twister's internal state, and every random(), randint(), choice() and shuffle() consumes 32-bit outputs from it in order — so the same seed replays the same sequence and a different seed gives a different one.

How it works

  1. random.seed(n) splits |n| into 32-bit words and runs MT19937's init_by_array, filling the 624-word state.
  2. Each 32-bit output takes the next state word and tempers it; after 624 words the whole state is regenerated (the twist).
  3. random() joins two outputs: with , , a float in [0.0, 1.0).
  4. randint(a, b) = a + _randbelow(b − a + 1); choice(seq) = seq[_randbelow(len(seq))]; _randbelow(n) keeps the top n.bit_length() bits and rejects values ≥ n.
  5. shuffle walks i from the end down to 1 and swaps x[i] with x[_randbelow(i + 1)].
  6. Replaying the program with a second seed shows where the sequences agree or first diverge.

Default run (11 steps): A program of 5 random-module calls, run after random.seed(42). … Replay with random.seed(42): every result is identical — same seed, same state, same sequence.

Simplified: Seeds are non-negative integers below (one key word), at most 8 calls, randint bounds within ±1000, and lists of up to 8 simple items. Only mt[0..3] and mt[623] of the 624-word state vector are shown (abbreviated). Everything else follows CPython 3.12's random module exactly.

Educational simulation

Loading the simulation…

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.

randint(5, 10)low and high included5included5included10includedrange(5, 10)high excluded10excludedincludesincludesincludesexcludes
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

The important distinction is the treatment of the high value.

Best practice

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.

Worked example

Generating a Random Rating

Generate one random integer rating from 1 through 5, including both 1 and 5.

  1. 1
    Identify the lower bound

    The smallest permitted rating is 1, so 1 becomes low.

  2. 2
    Identify the upper bound

    The largest permitted rating is 5, so 5 becomes high. Since randint() includes high, 5 remains a possible result.

  3. 3
    Import the module

    The random module must be imported before calling randint().

  4. 4
    Write the call

    Use random.randint(1, 5) to generate the integer.

Result

The program generates one random integer from 1 through 5.

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

Correcting Invalid Calls

  • Common mistake

    Forgetting to import the random module

    What it looks like
    number = random.randint(5, 10)
    Why it fails

    The call uses random, but the module has not been imported.

    Do this instead

    Add import random before calling random.randint().

  • Common mistake

    Treating the high value as excluded

    What it looks like

    Using random.randint(5, 9) when 10 is intended to be a possible result

    Why it fails

    randint() includes its high argument.

    Do this instead

    Use random.randint(5, 10) when the permitted interval includes 10.

  • Common mistake

    Passing the bounds in the wrong order

    What it looks like
    random.randint(10, 5)
    Why it fails

    The lower and upper roles have been reversed.

    Do this instead

    Place the smaller integer first and the larger integer second.

  • Common mistake

    Using non-integer arguments

    What it looks like
    random.randint(1.5, 5.5)
    Why it fails

    randint() requires both parameters to be integers.

    Do this instead

    Supply integer values for both low and high.

Integer intervalchoose low and highlow before highcheck argument rolesrandint(low, high)integer argumentsCorrect bound ordersmaller firstidentify boundscorrect orderlow greater than highretry with corrected order
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. 1
    random.randint(low, high) generates one random integer from an inclusive interval.
  2. 2
    Both low and high can be returned by randint().
  3. 3
    This differs from range(), whose upper endpoint is excluded.
  4. 4
    Import random before calling random.randint(), and pass integer arguments.
  5. 5
    Put the smaller bound first and verify that the high value is intended to be included.

At a glance

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

More concepts starting with U