Python Random randint() Method

Usage

The random.randint() method generates a random integer within a specified range.

The generated random numbers are not truly random in a cryptographic sense but are pseudorandom, which means they are generated using an algorithm with deterministic behavior based on the initial seed. If you need cryptographically secure random numbers, you should use the secrets module.

Syntax

random.randint(start, stop)

Parameters

ParameterConditionDescription
startRequiredAn integer that specifies the starting value of the range
stopRequiredAn integer that specifies the ending value of the range.

Return Value

The random.randint() method returns a random integer N, such that start <= N <= stop.

It should be noted that the generated random integer will include both start and stop.

Module Import

To use the random.randint() method, you’ll first need to import the random module.

import random

Examples

Simulating a dice roll is a simple example of using random.randint(). By setting the range from 1 to 6, you mimic the possible outcomes of a standard six-sided die.

import random

# Generate a random number between 1 and 6 (inclusive)
print(random.randint(1, 6))
# Possible output: 3

Another simple example is simulating a coin toss. Using random.randint(0, 1) gives you two equally likely outcomes, which we can interpret as “heads” or “tails”. This is perfect for situations in programs where you need to make a random binary decision.

import random

# Generate a random number between 0 and 1 (inclusive)
print(random.randint(0, 1))
# Possible output: 1

You’re not limited to simulating dice rolls or coin flips. random.randint() lets you specify any starting and ending points to produce random integers within that range.

# Generate a random number between 100 and 1000 (inclusive)
print(random.randint(100, 1000))
# Possible output: 766

# Generate a random number between -50 and 50 (inclusive)
print(random.randint(-50, 50))
# Possible output: -33

Generating Random Floating-Point Numbers

It’s important to remember that the random.randint() function specifically works with integers. If you try to use floating-point numbers as arguments, you’ll encounter a TypeError.

print(random.randint(3.4, 9.8))
# Triggers TypeError: 'float' object cannot be interpreted as an integer

To generate random floating-point numbers within a range, the Python random module offers two primary functions:

  • random(): This method generates a random float between 0.0 (included) and 1.0 (not included).
    import random
    print(random.random())
    # Possible output: 0.6660928176356694
  • uniform(start, stop): This method generates a random float number between two specified bounds.
    import random
    print(random.uniform(3.4, 9.8))
    # Possible output: 5.132250595111026

randrange() vs randint()

While both randrange() and randint() are useful for generating random integers, they have subtle differences in how they work.

  • randint(start, stop) returns a random integer within a completely inclusive range. This means both the start and stop values you provide can be included in the possible results.
  • On the other hand, randrange([start], stop[, step]) offers more flexibility. It generates a random integer within a range where the stop value is exclusive (meaning it won’t be included as a possible result). Additionally, the optional step argument allows you to specify the increments between potential outcomes.

In a nutshell, randint(start, stop) is equivalent to calling randrange(start, stop + 1).

So, randint() is straightforward and more suitable when you need a random integer within a specified range, including both endpoints. However, if you require more fine-grained control over the range, especially with regular intervals other than 1, randrange() is the preferable choice.