How to Use NumPy random.randint() in Python

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:January 10, 2024
  • Reading time:17 mins read

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.


# Quick examples of numpy random integer

# Example 1: Generate a single random integer 
# Between 1 and 100 (inclusive)
random_integer = np.random.randint(1, 101)

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

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

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

# Example 5: Generate a random integer as 'int64'
arr = np.random.randint(low=1, high=10, dtype='int64')

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 Parameters of random.randint()

Following are the parameters of random.randint().

  • low – Lowest (signed) integers, get from the distribution. All the numbers generated 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 generated 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

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).

3.1 Generate a Single Random Integer

To generate a single random integer using numpy.random.randint() function. For instance, In this example, np.random.randint(1, 101) generates a random integer between 1 and 100 (inclusive) and assigns it to the variable random_integer. You can adjust the range (1 and 101 in this case) according to your specific requirements. When you run this code, it will print a different random integer each time due to the nature of random number generation.


# Import numpy
import numpy as np

# Generate a single random integer 
# Between 1 and 100 (inclusive)
random_integer = np.random.randint(1, 101)
print("After generating random integers:", random_integer)

3.2 Generate an Array of Random Integers

To generate an array of random integers using numpy.random.randint(), you can specify the low, high, and size parameters. For example, np.random.randint(low=1, high=6, size=8) generates an array with 8 random integers between 1 and 5 (inclusive). You can adjust the low, high, and size parameters to generate arrays of different sizes and integer ranges as per your requirements. Each time you run the code, it will generate a new array of random integers due to the nature of random number generation.


# Get the random integers of array
arr = np.random.randint(low=1, high=6, size=8)
print("After generating random integers of the array:\n",arr)

Yields below output.

numpy random randint()

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 a 2-D NumPy array of a given shape. use the size param to provide the shape of the return array.

In the below example, np.random.randint(low=1, high=9, size=(2, 4)) generates a 2-D array with 2 rows and 4 columns, filled with random integers between 1 and 8 (inclusive). You can adjust the low, high, and dimensions of the array (specified in the size parameter) to generate 2-D arrays of different shapes and integer ranges according to your requirements. Each time you run the code, it will generate a new 2-D array of random integers due to the random number generation.


# Generate 2D numpy array
arr = np.random.randint(low=1, high=9, size=(2, 4))
print("After generating random integers of the 2D array:\n",arr)

# Output:
# After generating random integers of the 2D array:
#  [[6 7 4 3]
#  [5 8 3 1]]

5. Generate Random Integers of 3-D NumPy Array

You can generate a 3-D array of random integers using numpy. random.randint(), you can specify the low, high, and size parameters, where the size parameter is a tuple representing the dimensions of the array.

In the below example, np.random.randint(low=3, high=8, size=(2, 3, 4)) generates a 3-D array with dimensions 2x3x4, filled with random integers between 1 and 7 (inclusive). You can adjust the low, high, and dimensions of the array (specified in the size parameter) to generate 3-D arrays of different shapes and integer ranges according to your requirements.


# Generate randint 3D array
arr = np.random.randint(low=3, high=8, size=(2, 3, 4))
print("After generating random integers of the 3D array:\n",arr)

# Output:
# After generating random integers of the 3D array:
#  [[[3 6 4 4]
#   [5 5 3 5]
#   [3 3 3 6]]

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

6. Generate Random Integers with a Specific Data Type

You can generate random integers with a specific data type using the dtype parameter in the numpy.random.randint() function. For instance, the dtype='int64' parameter specifies that the random integer generated will have a data type of 64-bit integer ('int64'). You can replace 'int64' with other valid NumPy data types such as 'int32', 'int16', etc., to generate random integers with different data types according to your requirements.


# Generate a random integer as 'int64'
arr = np.random.randint(low=1, high=10, dtype='int64')
print("After generating random integer of data type:\n",arr)

# Output:
# After generating random integer of data type:
#  4

Frequently Asked Questions

What does numpy.random.randint() do?

numpy.random.randint() is a function in the NumPy library used to generate random integers. It allows you to generate random integers between specified low (inclusive) and high (exclusive) values. The generated integers can be used for various purposes such as simulations, random sampling, and generating random data for testing and analysis.

How can I generate a single random integer using numpy.random.randint()?

To generate a single random integer using numpy.random.randint(), you can provide the low and high parameters to specify the range within which you want the random integer to be generated.

How can I generate an array of random integers using numpy.random.randint()

To generate an array of random integers using numpy.random.randint(), you can provide the low, high, and size parameters. The low parameter specifies the lowest (inclusive) integer to be drawn from the distribution, the high parameter specifies the highest (exclusive) integer, and the size parameter specifies the shape of the output array.

Can I generate random integers with a specific data type using numpy.random.randint()?

You can generate random integers with a specific data type using the numpy.random.randint() function. You can specify the data type using the dtype parameter. By default, the data type is set to 'int', but you can change it to any valid NumPy data type, such as 'int32', 'int64', etc.

How can I generate random integers in a given shape using numpy.random.randint()?

You can generate random integers in a specific shape using the numpy.random.randint() function by providing the size parameter. The size parameter allows you to specify the dimensions of the output array.

Conclusion

In this article, I have explained the np.random.randint() method syntax, parameters, and usage of how to generate random integers of single and multi-dimensional NumPy arrays of specified shapes with examples.

Happy Learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply