Random sampling in numpy | randint() function Last Updated : 17 Nov, 2025 Comments Improve Suggest changes 11 Likes Like Report The numpy.random.randint() function is used to generate random integers within a specified range. It allows you to create arrays of any shape filled with random integer values, making it useful in simulations, testing, and numerical experiments.Example:Input: Generate integers between 0 and 5Output: [3 1 4 0 2]Explanation: Each value is a random integer from the interval [0, 5).Syntaxnumpy.random.randint(low, high=None, size=None, dtype=int)Parameters:low: lowest integer that can appear in the output.high(Optional): Upper limit (exclusive). If omitted, range becomes [0, low).size(Optional): Shape of the output array (e.g., 5, (2,3), (2,3,4)).dtype(Optional): Data type of the returned numbers. Default is integer.ExamplesExample 1: This example generates five random integers between 0 and 4, stored in a one-dimensional array. Python import numpy as np arr = np.random.randint(0, 5, size=5) print(arr) Output[2 2 3 3 4] Example 2: This example creates a 2×3 matrix of random integers ranging from 0 to 9. Python import numpy as np arr = np.random.randint(0, 10, size=(2, 3)) print(arr) Output[[7 9 8] [9 1 1]] Explanation:size=(2, 3): creates 2 rows and 3 columns.0 to 10: upper limit 10 is excluded.Example 3: This example produces a 3D array (2×2×4) with values between 5 and 15. Python import numpy as np arr = np.random.randint(5, 15, size=(2, 2, 4)) print(arr) Output[[[ 8 7 12 12] [ 6 13 13 6]] [[11 6 6 7] [ 7 9 6 8]]] Create Quiz Comment J jana_sayantan Follow 11 Improve J jana_sayantan Follow 11 Improve Article Tags : Python Python-numpy Python numpy-Random sampling Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like