How to Use NumPy random.randint() in Python

NumPy random.randint() function in Python is used to return random integers from the values specified with low (inclusive) to high (exclusive) param. It creates an array of a given shape and fills it with random integers from low (inclusive) to high (exclusive).

This function takes a tuple as an integer to specify the size of an array and the behavior of this function is same as the other NumPy functions like the numpy.ones() and numpy.zeros(). In this article, I will explain how to use the NumPy random.randint() function and using its syntax and parameters how we can generate random integers between two values.

1. Quick Examples of NumPy Random Integer

If you are in a hurry, below are some quick examples of how to use Python NumPy random integer.


# Below are the quick examples

# Example 1: Get the 1-D array of random integers
arr = np.random.randint(low=1, high=6, size = 8)

# Example 2: Get the 2-D array of random integers
arr = np.random.randint(low=1, high=9, size=(2, 4))

# Example 3: Get the 3-D array of random integers
arr = np.random.randint(low=3, high=8, (2, 3, 4))

2. Syntax of NumPy random.randint()

Following is the syntax of random.randint() function.


# Syntax of random.randint() 
random.randint(low, high=None, size=None, dtype=int)

2.1 Prameters of random.randint()

Following are the parameters of random.randint().

  • low – Lowest (signed) integers, get from the distribution. All the numbers generate using randint() are greater than or equal to low.
  • high – The largest (signed) integers, get from the distribution. It is the upper bound. All the numbers generate from randint() are less than high.
  • size – Integer or tuple of integers. Using this to specify the array size or dimension. For instance, size=(3,4) fills three rows and four columns array with integers.
  • dtype – Desired dtype of the result.

2.2 Return Value of randint()

It returns a size and shaped array of random integers in the interval [low, high), or by default returns a single random integer.

3. Usage of NumPy random.randint()

The random.randint() is a NumPy library function that returns an array of random integers that are discrete uniform distribution of the specified dtype in the half-open interval [low, high). By default the value of high is None. If no value is used for high param then the results are from [0, low).


import numpy as np

# Get the random integers of array
arr = np.random.randint(low=1, high=6, size = 8)
print(arr)

# Output
# [5 3 5 5 4 3 1 5]

4. Generate 2-D Array of Random Integers

Create two-dimensional random arrays by providing a default lower limit and provide high limit to this function. It will return the random integers of 2-D NumPy array of a given shape. use size param to provide the shape of the return array.


# Generate 2D numpy array
arr = np.random.randint(low=1, high=9, size=(2, 4))
print(arr)

# Output
# [[4 8 6 3]
# [7 5 4 4]]

5. Generate Random Integers of 3-D NumPy Array

You can generate the 3-dimensional array of random numbers by providing a tuple of integers into this function. It will return the random integers of a 3-D array of the specified shape.


# Generate randint 3D array
arr = np.random.randint(low=3, high=8, (2, 3, 4))
print(arr)

# Output
# [[[5 3 7 5]
#  [5 6 7 4]
#  [7 7 6 7]]

# [[6 4 6 6]
#  [3 4 4 7]
#  [4 6 7 6]]]

6. Conclusion

In this article, I have explained np.random.randint() function and using this how we can generate random integers of single and multi-dimensional NumPy array of specified shape with python examples.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing How to Use NumPy random.randint() in Python