Tutorial

The randint() Method in Python

Clear, original guide to python randint: syntax, inclusive bounds, examples, seeding, randrange comparison, error handling, and practical tips.

Drake Nguyen

Founder · System Architect

3 min read
The randint() Method in Python
The randint() Method in Python

Introduction

This guide explains python randint from the built-in python random module. You will learn the syntax, how python randint treats its bounds, common use cases such as generating a python random integer between two numbers, and how to avoid typical errors like ValueError when passing non-integers. Examples are included for single calls, multiple calls in a loop, and using random.seed for reproducible results.

randint() Syntax and Behavior

The function random.randint(a, b) returns a pseudo-random integer N such that a <= N <= b. In other words, python randint is inclusive of both endpoints. Under the hood, random.randint is an alias for random.randrange(a, b+1), so the upper bound is included.

Basic form

import random
# randint(lower, upper) — both ends included
n = random.randint(1, 10)
print(n)  # outputs an integer from 1 to 10 inclusive

Examples

Generate a random integer between 1 and 10

import random
print(random.randint(1, 10))  # python random randint between 1 and 10

Multiple calls in a loop

Calling python randint repeatedly will typically produce different values, though repeats (collisions) can occur if the range is small relative to the number of draws.

import random
for i in range(5):
    print(random.randint(10, 100))  # python randint multiple calls loop

Reproducible sequences with seed

Use random.seed() when you need repeatable results from the pseudo random number generator. Seeding sets the PRNG state so subsequent random.randint calls produce the same sequence.

import random
random.seed(42)           # random seed python
print(random.randint(1, 6))
print(random.randint(1, 6))
# Running this block again with the same seed will produce the same two values

randrange vs randint

Both functions come from the python random module. The key difference is that random.randrange accepts a start/stop semantics similar to range(), where the stop is exclusive. random.randint(a, b) is equivalent to random.randrange(a, b + 1) and is therefore inclusive at both ends. This distinction answers questions like python randrange vs randint difference and python random randint inclusive vs exclusive behavior.

Error Handling and Limits

random.randint requires integer arguments. Passing floats or non-integer types raises a ValueError (internally from randrange) with a message like "non-integer arg 1 for randrange()". Also be aware of integer bounds if you work with extremely large values.

import random
# This will raise ValueError: non-integer arg 1 for randrange()
beg = 5.3
end = 10.2
print(random.randint(beg, end))  # python randint float error

Practical Tips and Use Cases

  • For inclusive ranges, use random.randint(a, b) — it returns integers including both a and b (randint python inclusive both ends).
  • If you need a uniform float instead of an integer, consider random.uniform or random.random rather than randint (uniform vs randint python).
  • To select multiple items or weighted choices, prefer random.choices over repeated calls to randint (choices vs randint python).
  • When generating many values in a tight range, expect collisions. Use sampling methods like random.sample for unique selections.

Conclusion

random.randint is a simple, commonly used tool for generating python random integers. It is inclusive of both bounds, ties to randrange internally, and relies on the pseudo-random generator in the python random module. Use random.seed for reproducible output and ensure arguments are integers to avoid ValueError. For more advanced needs (floats, weighted choices, or non-repeating samples), explore other functions in the same module.

References

  • Official docs: Python's random module provides randrange, randint, seed, uniform, and choices.
  • Stack Overflow and community tutorials for examples and troubleshooting common errors like non-integer arg 1 for randrange.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.