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]]
numpy_array_creation_routines.htm
Advertisements