
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
Numpy random.randint() Function
The Numpy random.randint() function is used to generate random integers from a specified range. It can generate either a single random integer or an array of random integers. This function is useful for creating random integer data for simulations, testing, or populating arrays.
The random.rand() function and the numpy.random.randint() function both are used to generate random values. The key difference between them is that the numpy.random.rand() function generates float values between 0 and 1, while the numpy.random.randint() function generates integer values within a specified range.
Syntax
Following is the syntax of the Numpy random.randint() function −
numpy.random.randint(low, high=None, size=None, dtype=int)
Parameters
Following are the parameters of the Numpy random.randint() function −
- low: An integer specifying the lower bound of the range (inclusive).
- high(optional): An integer specifying the upper bound of the range (exclusive). If this parameter is not provided, low is treated as the upper bound, and the lower bound is set to 0.
- size(optional): An integer or tuple of integers specifying the output shape. If size is not specified, a single integer is returned.
- dtype(optional): The desired data type of the output array. The default value is int.
Return Values
This function returns a NumPy array of random integers from the specified range.
Example
Following is a basic example to generate a single randome integer using Numpy random.randint() function −
import numpy as np Random_int = np.random.randint(0, 10) print("Random Integer:", Random_int) print(type(Random_int))
Output
Following is the output of the above code −
Random Integer: 3 <class 'int'>
Example : Generating a NumPy Array
Using numpy.random.randint() function, we can also generate a 1D NumPy array of random integers.
Here, we generate a 1D array of size 5 with random integers between 10 and 50.
import numpy as np Random_Numpy_Array = np.random.randint(10, 50, size=5) print("Random 1D Array:", Random_Numpy_Array) print(type(Random_Numpy_Array))
Output
Following is the output of the above code −
Random 1D Array: [33 40 16 49 30] <class 'numpy.ndarray'>
Example : Multi-Dimensional NumPy Array
We can generate a multi-dimensional NumPy array by setting the size parameter to a tuple in the numpy.random.randint() function.
In the following example, we generate a 3D array of random integers between 50 and 100, with dimensions 4x4.
import numpy as np Random_3d_array = np.random.randint(50, 100, size=(4, 4)) print("Random 3D Array:\n", Random_3d_array)
Output
Following is the output of the above code −
Random 3D Array: [[87 60 71 85] [94 88 92 94] [61 73 95 92] [74 76 95 80]]
Example : 'Random.rand()' vs 'Random.randint()'
The numpy.random.randint() function is used to generate random integer values or a NumPy array of integers within a specified range. On the other hand, the numpy.random.rand() function generates a NumPy array of random float values between 0 and 1.
The following example demonstrates both numpy.random.randint() and numpy.random.rand() functions:
import numpy as np # Generating a 2D array of random integers using randint random_int_array = np.random.randint(1, 100, size=(3, 3)) print("Random 2D Integer Array (1 to 99):\n", random_int_array) # Generating a 2D array of random floats using rand random_float_array = np.random.rand(3, 3) print("\nRandom 2D Float Array (0 to 1):\n", random_float_array)
Output
Following is the output of the above code:
Random 2D Integer Array (1 to 99): [[98 15 39] [21 60 76] [47 9 85]] Random 2D Float Array (0 to 1): [[0.6788924 0.40933523 0.94536588] [0.51462956 0.97583621 0.24382739] [0.65600153 0.12382789 0.08912578]]