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

Code walkthrough
import random

roll = random.randint(5, 10)
  1. 1
    The import statement makes the random module available to the program.
  2. 2
    The call supplies two integer arguments: the low boundary 5 and the high boundary 10.
  3. 3
    The returned value is assigned to roll.
  • Import the random module before using randint().
  • Generate one integer from 5 through 10, including both 5 and 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.

randint(1, 6)includes 1 through 61, 2, 3, 4, 5, 66 includedrange(1, 6)excludes 61, 2, 3, 4, 56 excludedpossible integersvalues in sequence
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

The upper-bound rule is the key difference between these two functions.

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

Worked example

Representing a Six-Sided Die Roll

Generate one random integer representing a result from 1 through 6.

  1. 1
    Identify the boundaries

    The smallest allowed result is 1 and the largest allowed result is 6.

  2. 2
    Use inclusive bounds

    Call random.randint(1, 6), because both 1 and 6 must be possible results.

  3. 3
    Interpret the result

    The returned integer represents one of the six possible outcomes.

Result

The call random.randint(1, 6) represents a random integer choice from 1 through 6.

python

Boundary Mistakes

missing importbeforerandom.randint(low,high)imported module; twointeger boundshigh, lowbeforenon-integer boundsbeforeimport randomput low firstuse integers
What changes when the import, bounds, or argument types are incorrect, and what does the corrected call require?
  • Common mistake

    Forgetting to import the random module

    What it looks like

    Calling randint() without first importing random

    Why it fails

    The source requires the random module to be imported before calling randint().

    Do this instead

    Begin with import random and call random.randint(low, high).

  • Common mistake

    Treating the high value as excluded

    What it looks like

    Assuming random.randint(1, 6) can return only 1 through 5

    Why it fails

    randint() includes both its low and high endpoints.

    Do this instead

    Include the high value when listing the possible results.

  • Common mistake

    Passing the bounds in the wrong order

    What it looks like

    Using the high boundary first and the low boundary second

    Why it fails

    The function expects the low boundary followed by the high boundary.

    Do this instead

    Place the smaller or low boundary first and the larger or high boundary second.

  • Common mistake

    Using non-integer arguments

    What it looks like

    Supplying bounds that are not integers

    Why it fails

    Both parameters should be integers.

    Do this instead

    Choose integer values for both boundaries.

  • Common mistake

    Omitting a boundary

    What it looks like

    Trying to specify only one boundary

    Why it fails

    The randint() call is described with both a low and a high parameter.

    Do this instead

    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. 1
    random.randint(low, high) returns a random integer including both low and high.
  2. 2
    random.randint(1, 6) can return six possible values: 1, 2, 3, 4, 5, and 6.
  3. 3
    Unlike randint(), range() excludes its upper endpoint.
  4. 4
    Import random before calling random.randint(), and provide two integer parameters in low-to-high order.
  5. 5
    Use randint() when a real-world choice can be represented by an integer interval.

At a glance

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

More concepts starting with S