Python:NumPy randint()
Anonymous contributor
Published Oct 30, 2025
Contribute to Docs
randint() is a function from NumPy’s random module that generates random integers. It can generate a single integer or an array of integers within a specified range, making it useful for simulations, testing, and randomized operations.
Syntax
numpy.random.randint(low, high=None, size=None, dtype=int)
Parameters:
low(int): Lowest (inclusive) integer to be drawn.high(int, optional): One above the highest integer to be drawn. If not provided, integers are drawn from the range[0, low).size(int or tuple of ints, optional): Output shape. IfNone, a single integer is returned.dtype(data-type, optional): Desired data type of the output. Default isint.
Return value:
out(int or ndarray): Random integer(s) from the specified range.- If
sizeisNone, returns a single integer. - If
sizeis specified, returns a NumPy array of the given shape.
- If
Example
This example generates a random integer (0-9) and an array of 5 random integers (1-5):
import numpy as npnp.random.seed(15)# Single random integer from 0 to 9single_int = np.random.randint(10)print(single_int)# Array of 5 random integers from 1 to 5arr = np.random.randint(1, 6, size=5)print(arr)
The output for this code will be:
8[5 1 5 4 4]
Note: Exact output values may vary depending on NumPy version, but the format will be as shown.
Here:
np.random.seed(15)ensures reproducible results.np.random.randint(10)generates a single integer between 0 and 9.np.random.randint(1, 6, size=5)generates an array of 5 integers between 1 and 5.
Codebyte Example
This codebyte sample generates a single random integer and a 2×3 array of random integers from specified ranges, ensuring reproducible results using a fixed random seed:
np.random.seed(42)ensures the code produces the same results every time.size=(2, 3)generates a 2D array with 2 rows and 3 columns of random integers.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours