# Numpy.random.randint() in python

numpy.random.randint function is used to get random integers from low to high values. The low value is included while the high value is excluded in the calculations. The output values are taken from the discrete uniform distribution of the range values.

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

• Purpose: The numpy random randint function used for creating a numpy array with random integers from low to high interval.

• Parameteres:

• low: int or array-like of ints: Lowest integers are drawn from the random values.
• high: int or array-like of ints, optional: Larger or highest integers are drawn from the random values.
• size: int or tuple of ints, optional: The shape of the array you want to create.
• dtype: dtype, optional The type of the output you want.
• Returns: out:int or ndarray of ints Returns an array with specified shape along with random integer values in it.

# Import Packages
import numpy as np
import warnings

warnings.filterwarnings("ignore")


## 1.Construct 1-D array using randint() function

In this example you will understand how to construct a 1-D array using np.random.randint() function

### Example 1: Create a 1-D array using randint()

# create a 1-D array
a = np.random.randint(low = 1, size = 5)

# print the newly created array
print(a)

[0 0 0 0 0]

• when low = 1 then randint() takes integer as 0.
• The size = 5 which creates a 1-D array
• Now a 1-D array is created with integer as 0.

### Example 2: Create a 1-D array using randint()

# create a 1-D array
a = np.random.randint(low = 1, high = 6, size = 8)

# print the newly created array
print(a)

[2 1 1 1 3 3 3 1]

• when low = 1 and high = 6 randint() takes integers between 1 to 5.
• The size = 8 which creates a 1-D array
• Now a 1-D array is created with integers between 1-5.

## 2.Construct 2-D array using randint() function

In this example you will understand how to construct a 2-D array using np.random.randint() function

### Example 1: Create a 2-D array using randint()

# create a 2-D array
a = np.random.randint(low = 2, size = (2,3))

# print newly created array
print(a)

[[0 1 0]
[0 0 0]]

• when low = 2 then the randint() takes integers between 0 to 1.
• As you set the size as (2,3), which creates a 2-D array with 2 rows and 3 columns
• Now the 2-D array is created with integers between 0-1

### Example 2: Create a 2-D array using randint()

# create a 2-D array
a = np.random.randint(low = 0, high = 6, size = (3,4))

# print newly created array
print(a)

[[3 2 3 5]
[1 4 5 2]
[3 5 1 2]]

• The low = 0 and high = 6 then the randint() takes integers between 0 to 5.
• As you set the size as (3,4), which creates a 2-D array with 3 rows and 4 columns.
• Now the 2-D array is created with integers between 0-5

## 3.Construct 3-D array using randint() function

In this example you will understand how to construct a 3-D array using np.random.randint() function

### Example 1: Create a 3-D array using randint()

# create a 3-D array
a = np.random.randint(low = 0, high = 5, size = (2,3,4))

# print newly created array
print(a)

[[[0 0 2 4]
[3 2 3 4]
[1 3 3 1]]

[[3 3 0 3]
[0 0 1 1]
[2 4 2 3]]]


Note When creating a 3-D array you have to specify both high and low parameters. If you specify only low parameter it returns a error.

Try the above scenario to know what kind of error it returns.

• When is set to low = 0 and high = 5 the randint() takes integers between 0 to 4.
• As you set the size as (2,3,4) which creates a 3-D array. with two sub arrays, three rows and four columns for each sub array.
• Now the 3-D array is created with integers between 0-4

## 4.Test your knowledge

Q1: Can you create a 3-D arrays without defining high parameter?
Ans: No, you cannot create a 3-D array without defining a high parameter. That returns a value error.

Course Preview