Concepts / random.randint() for Random Integers

random.randint() for Random Integers

random.choice() selects one element at random from a sequence by generating a random index and returning the element at that position.

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

One Call, One Element

Suppose a sequence contains several possible values and your program should select one of them. random.choice() performs that selection. It returns exactly one element from the sequence, but the selected element can differ from one call to the next.

python

From Index to Value

random.choice() selects an element by using a random index. For a sequence of length three, the possible indices are 0, 1, and 2. The function generates one of those index values and returns the element stored at that position. The index is the position used for selection; the returned result is the element found there.

Index 01Random index1Returned value2Index 12Index 23selects positionreturns value
How does random.choice() map a randomly generated index to one specific element in a sequence?
Worked example

Following One Selection

A list contains 1, 2, and 3. What value is returned if random.choice() selects index 1?

  1. 1
    Arrange the sequence

    The list contains 1 at index 0, 2 at index 1, and 3 at index 2.

  2. 2
    Choose an index

    The selection happens at index 1.

  3. 3
    Read the element

    The element at index 1 is 2, so random.choice() returns 2.

Result

The returned value is 2.

Tracing Repeated Calls

What do you think happens?

The list t contains 1, 2, and 3. If the first call to random.choice(t) returns 2 and the second returns 3, what can happen on a third call?

  • It must return 1

  • It must return 3

  • It can return 1, 2, or 3

Reveal answer
Answer:

It can return 1, 2, or 3.

Each call independently generates a fresh random index. A third call may return any element, including an element returned by an earlier call.

Code walkthrough
import random

t = [1, 2, 3]
first = random.choice(t)
second = random.choice(t)
print(first)
print(second)
  1. 1
    The random module is imported so random.choice() is available.
  2. 2
    The name t refers to a three-element list.
  3. 3
    The first call selects one element from t.
  4. 4
    The second call performs a new independent selection from t.
  5. 5
    The displayed values are one possible result, not a guaranteed result for every run.
  • The list has three possible positions: 0, 1, and 2.
  • The first call can select index 1 and return 2.
  • The second call can select index 2 and return 3.
Output
One possible output:
2
3
Receive sequence1, 2, 3Generate index0, 1, or 2Look up positionindex 1Return element2sequence length determines possible positionsuses indexreads value
What happens step by step when random.choice() receives a sequence, generates an index, and returns the selected value?

The important tracing habit is to separate the random position from the value stored at that position. First identify the sequence and its indices. Then identify the selected index. Finally read the element at that index. A later call starts the process again with a fresh random index.

Sequences and Selection

random.choice() receives a sequence and returns one element from it. A list is a common example. The selected position determines the returned content, so changing the sequence's contents changes the possible results, while the selection mechanism remains the same.

python
List10, 20, 30, 40Other sequenceelements in orderSelected positionone indexOne elementvalue at that positionprovides positionsprovides positionsreturns value
What kinds of sequences can random.choice() receive, and how does the selected position relate to the sequence's contents?

choice() and choices()

FunctionHow many elementsReturn shapeReplacement behavior
random.choice()Exactly oneThe selected element directlyA later independent call can select the same element
random.choices()One or moreA list of selected elementsAllows sampling with replacement, so an element can be selected multiple times

The key differences between random.choice() and random.choices().

random.choice()one selectionOne elementdirect resultReplacementrepeated elements allowedrandom.choices()one or more selectionsListselected elementsreturnsreturnsallows
How does selecting one element with random.choice() differ from selecting multiple elements with random.choices(), including replacement and output shape?
python

The function name's final s matters. random.choice() returns one selected element directly. random.choices() returns selected elements in a list and allows the same element to appear more than once through sampling with replacement.

Random Integer and Element

Inside random.choice(), the generated random integer is used as an index. For a sequence with three elements, the possible index values are 0, 1, and 2. The function does not return that index as its normal result; it uses the index to return the element at that position. This is the essential difference between an index and the selected sequence element.

Random integerindex 1Selected element2Sequence1, 2, 3acts as positionreturns value at position
How does the random integer used for a position differ from the element returned from a sequence?

Mistakes Beginners Make

  • Common mistake

    Using random.choice() without importing random.

    What it looks like
    selected = random.choice(items)
    Why it fails

    The random module must be imported before random.choice() can be used.

    Do this instead

    Add import random before the call.

  • Common mistake

    Expecting random.choice() to return an index.

    What it looks like
    items = ["A", "B", "C"]
    selected = random.choice(items)
    Why it fails

    The generated integer is used internally as an index; the function returns the element at that position.

    Do this instead

    Treat selected as one element from items.

  • Common mistake

    Assuming repeated calls cannot return the same element.

    What it looks like
    first = random.choice(items)
    second = random.choice(items)
    Why it fails

    Each call is independent, so the same element may be selected multiple times by chance.

    Do this instead

    Allow each call to produce any element in the sequence.

  • Common mistake

    Confusing random.choice() with random.choices().

    What it looks like
    random.choice(items) and random.choices(items)
    Why it fails

    random.choice() selects one element directly, whereas random.choices() returns selected elements as a list and allows sampling with replacement.

    Do this instead

    Use random.choice() for one element and random.choices() when multiple selected elements in a list are required.

Practice the Trace

EASY

Consider the sequence values = ["north", "south", "east"]. Trace a call to random.choice(values) when the generated index is 2. State the returned element. Then explain why a second call could return the same element or a different one.

Hints
  • Write the indices beside the values, starting with index 0.
  • Use the element at index 2 as the result.
  • Remember that each call generates a fresh random index.
Worked example

Practice Solution

For values = ["north", "south", "east"], random.choice(values) uses index 2.

  1. 1
    Map positions

    The value north is at index 0, south is at index 1, and east is at index 2.

  2. 2
    Read the selected position

    Index 2 refers to east.

  3. 3
    Trace another call

    A later call independently generates a new random index, so it can select east again or select another element.

Result

The first call returns east. A later call may return east, north, or south.

Key Takeaways

  1. 1
    random.choice() selects exactly one element from a sequence.
  2. 2
    It generates a random integer that acts as an index and returns the element at that position.
  3. 3
    Each call is independent, so repeated calls can return the same element.
  4. 4
    Import the random module before using random.choice().
  5. 5
    random.choices() differs by returning multiple selected elements in a list and allowing sampling with replacement.

At a glance

  • random.choice() returns one randomly selected element from a sequence.
  • The selection process uses a random index between 0 and the sequence length minus 1.
  • The selected index and the returned element are different ideas: the index identifies a position, while the element is the value at that position.
  • Independent calls may return the same element.
  • random.choices() returns a list of one or more selections instead of one direct element.

Related concepts

More concepts starting with R