np.random.randint – Python NumPy random.randint() Function

np.random.randint: The random module is part of the NumPy library. This module includes the functions for generating random numbers. This module includes some basic random data generating methods, as well as permutation and distribution functions and random generator functions.

NumPy random.randint() Function:

Numpy random int: The randint() function of the random module is used to produce random integers ranging from inclusive (low) to exclusive (high).

Syntax:

numpy.random.randint(low, high=None, size=None, dtype='l')

Parameters

low: This is Required. It specifies the lowest signed integer to be taken from the distribution (unless high=None, in which case this argument is one above the highest such integer).

high: This is optional. If provided, one above the largest signed integer from the distribution will be drawn (check for the behavior if high=None).

size: This is optional. Set the output shape. If the supplied shape is (a, b, c), for example, a * b * c samples are drawn. The default value is None, which results in a single item being returned.

dtype: This is Optional. It specifies the desired result dtype.

Return Value:

np random int: Random int values from the proper distribution in the given shape, or a single such random int if no size is specified is returned.

NumPy random.randint() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass the low value, size as arguments to the numpy.random.randint() function to generate random integers of a specified shape([0, low)).
  • Store it in a variable
  • Pass the low value, size(rowsize*columnsize) as arguments to the numpy.random.randint() function to generate random integers of a specified shape([0, low).
  • Store it in another variable.
  • Print the above generated random numbers.
  • Print the above generated random numbers in a given rowsize*columnsize.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass the low value, size as arguments to the numpy.random.randint() function
# to generate random integers of a specified shape([0, low))
# Store it in a variable
p = np.random.randint(4, size=(12))
# Pass the low value, size(rowsize*columnsize) as arguments to the numpy.random.randint() function
# to generate random integers of a specified shape([0, low)) 
# Store it in another variable
q = np.random.randint(4, size=(4, 4))
# Print the above generated random numbers
print("The above generated random numbers\n", p)
# Print the above generated random numbers in a given rowsize*columnsize
print("The above generated random numbers in a given rowsize*columnsize\n", q)

Output:

The above generated random numbers
[3 3 3 0 1 0 3 2 3 1 3 2]
The above generated random numbers in a given rowsize*columnsize
[[1 1 3 0]
 [2 2 1 1]
 [0 0 0 1]
 [2 2 1 3]]

Example2

Numpy.random.randint: Here high value is given. Hence the function returns random numbers in a range [low, high).

Approach:

  • Import numpy module using the import keyword.
  • Pass the low, high, size values as arguments to the numpy.random.randint() function to generate random integers of a specified shape([low, high)).
  • Store it in a variable.
  • Pass the low, high, size(rowsize*columnsize) values as arguments to the numpy.random.randint() function to generate random integers of a specified shape ([low, high)).
  • Store it in another variable.
  • Print the above generated random numbers.
  • Print the above generated random numbers in a given rowsize*columnsize.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass the low, high, size values as arguments to the numpy.random.randint() function
# to generate random integers of a specified shape([low, high))
# Store it in a variable
p = np.random.randint(2, 15, (10))
# Pass the low, high, size(rowsize*columnsize) values as arguments to the
# numpy.random.randint() function to generate random integers of a specified shape
# ([low, high)) 
# Store it in another variable
q = np.random.randint(5, 10, (4, 4))
# Print the above generated random numbers
print("The above generated random numbers\n", p)
# Print the above generated random numbers in a given rowsize*columnsize
print("The above generated random numbers in a given rowsize*columnsize\n", q)

Output:

The above generated random numbers
[13 3 8 11 14 3 5 7 12 14]
The above generated random numbers in a given rowsize*columnsize
[[5 9 9 9]
 [8 7 7 6]
 [6 5 9 7]
 [9 6 7 6]]