Concepts / Choosing Random Elements from a List with random.choice()

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.

  • Programming

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?

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

nextnextnextnextnext5possible result6possible result7possible result8possible result9possible result10possible result
Which integer outcomes belong to random.randint(5, 10), including what happens at both 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.

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

ExpressionLower endpointUpper endpointPossible values in the example
random.randint(5, 10)IncludedIncluded5, 6, 7, 8, 9, 10
range(5, 10)IncludedExcluded5, 6, 7, 8, 9
includes upper endpointstops before upper endpointrandint(5, 10)5 through 1010includedrange(5, 10)5 through 910excluded
Which values can each expression produce, and what happens to the upper endpoint?

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.

may selectmay selectmay selectmay selectmay select0redrandint(0, 4)one valid position1blue2green3yellow4purple
How do inclusive endpoints map to the valid zero-based positions of a list?

import random colors = ["red", "blue", "green", "yellow", "purple"] position = random.randint(0, 4) selected_color = colors[position] print(selected_color)

ApproachWhat is produced firstWhat happens next
random.randint() with a list positionA random integerThe integer is used as a list position
random.choice()A selected list elementThe 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.

python
translate boundsreturnsRequested range1 through 6randint(1, 6)inclusive boundsRandom integer1, 2, 3, 4, 5, or 6
How does a real-world request become a randint() call and then a usable integer?

Mistakes with randint()

correct the ordercorrect the inclusive upper boundrandint(10, 5)reversed boundsrandint(0, 5)upper bound may exceedfinal positionrandint(5, 10)low before highrandint(0, 4)matches positions 0 through4
What changes when the module is imported and the randint() bounds are chosen deliberately?
  • 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

EASY

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

  1. random.randint(low, high) returns a random integer from low through high.
  2. Both endpoints are included, so random.randint(5, 10) can return 5, 6, 7, 8, 9, or 10.
  3. Unlike randint(), range() excludes its upper endpoint.
  4. Import random first, use integer arguments, and place the lower bound before the higher bound.
  5. 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.