Generate Random Integers using Generators in Numpy

In this post, we will learn how to generate random integers using Random Generators in Numpy Python. Random Generator class in Numpy offers wide variety of random distributions including integers.

To get started let us import Numpy

import numpy as np

To generate random numbers, we need a construcor of random number class Generator. In Numpy, default_rng() is the recommended constructor for generating random numbers.

Example 1: Generating a random integer

As a first example, let us generate a random integer. For generating random number, we need to create an instance of Random Generator using default_rng() as shown below.

# an instance of random. generator
rng = np.random.default_rng()

Now we can use the instance to generate any random number we want. Here we will generate a random integer using integers() function.

To generate a random integer between 0 and 9 (inclusive), we need to specify low and high arguments to integers() function. each time the code is run. In this example, our low is 0 and. high is 10. And this will generate a random integer between 0 and 9, including 9.

rng.integers(low=0, high=10)
8

Example 2: Generating an array of random integers

Similarly to generate an array of N random integers, we need to specify N using size argument inn addition to low and high argument. Note we can use the same random generator instance.

Here we have generated a 1D array of size 5.

rng.integers(low=0, high=10, size=5)
array([4, 1, 9, 9, 9])

In another example, we are generating 10 random integers between 0 and 9.

np.random.randint(0, 10, size=10)
array([2, 2, 6, 1, 3, 9, 6, 1, 0, 1])

Example 3: Generating a 2D array of random integers

By specifying the size argument to integers() function as tuple we can generate 2D array of random integers. Here the first element of the tuple specifies the number of rows and the second element specifies the number of columns.
In the example below, we generate matrix of random integers of dimension 3 x 5 with numbers between 0 and 9.

rng = np.random.default_rng()
rng.integers(low=0, high=10, size=(3,5))

[[9 6 1 0 1]
 [9 0 0 9 3]
 [4 0 0 4 1]]

With integers() function, we can generate 3DD Numpy array as well. In the example below we generate a 3D matrix with three element tuple for the size argument.

random_3D_array = rng.integers(0, 10, size=(2,3,5))
print(random_3D_array)
[[[2 7 8 8 8]
  [0 5 2 2 2]
  [8 7 2 4 7]]

 [[1 6 4 9 7]
  [2 8 7 2 5]
  [7 2 2 1 0]]]

Example 4: Generating a random integers with seed

Oftenw hen we generate random numbers, we would like to reproduce the same results. To reproduce same random numbers we need to specify a random seed, an integer, as argument to the constructor default_rng() function.

In. the. examples below we use seed=9 to generate a random integer

rng = np.random.default_rng(seed=9)
rng.integers(low=0,high=10)
9

to generate a 1D array of random integers

rng = np.random.default_rng(seed=9)
rng.integers(low=0,high=10, size=5)
array([4, 8, 9, 2, 1])

and to generate a. 2D array of random integers.

rng = np.random.default_rng(seed = 9)
rng.integers(low=0,high=10, size=(3,5))

array([[0, 6, 5, 0, 9],
       [2, 2, 1, 3, 1],
       [3, 8, 4, 9, 4]])

By using the same seed will generate the same random integers when we run the code again.