AppDividend
Latest Code Tutorials

np.random.randint: Random Numbers in Numpy

Generating random numbers is easy in Python because it provides a random and numpy package. When working with machine learning projects, you need to create a basic data set that consists of random integers and numbers. Using np.random.randint() function, you can create a random integer number.

np.random.randint

The np.random.randint() is a numpy function that generates the random integer number. The np.random.randint() function is present inside the unexpected class of the numpy package.

Syntax

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

Arguments

low: This is the starting range of the number. It is the lowest integer that should be generated from this function. If 1 is given in the low argument, this function generates a number greater than or equal to 1 but not less than 1.

high: This is the ending range of the number. It is the highest integer number generated from the np.random.randint() function. By default, it is set to None. Hence, np.random.randint() function generates a random number up to infinity. If the high argument is fixed with some value n, the np.random.randint() function generates a number less than or equal to n. The numbers will not be generated greater than n.

size: This is the output element size. If a single dimension is provided, a single list is created with the specified size. If multi-dimensions are given, it produces a multidimensional list or tuple. The size can be given as ( m, n, k ). Then m * n * k values are generated.

dtype: This is the data type of the output generated values. This stands for the data type. This is set to int by default.

Python program for creating a random integer number

import numpy as np

# Creating variable for storing the random integer
res = np.random.randint(10)
print(res)

Output

4

We imported the numpy library to use the numpy functions in this program. We used the randomint() function that generates a random integer.

In this program, we give the end limit as 10. Hence, the numbers will be generated between 0 t0 10. Finally, we printed the integer number. This number keeps on changing every time you run the program because it returns a random number.

Python program for creating a series of a random integer number

import numpy as np

# Creating variable for storing the random integer

res = np.random.randint(5, 10, (3, 3, 3))

# Printing the shape of the created random integer array
print(res.shape)
print(res)

Output

(3, 3, 3)

[[[8 6 7]
 [8 5 5]
 [9 9 6]]

 [[5 5 5]
 [9 7 7]
 [8 8 6]]

 [[6 9 8]
 [7 9 6]
 [7 6 7]]]

In this program, we create a matrix of 3 X 3 X 3 consisting of random integers. The range of the integers ranges from 5 to 10. The size of the resultant array is 3 X 3 X 3. 

If you run the above program every time, the output keeps changing. Since it is random, the values in the array keep changing every time we run this program.

That’s it for the numpy random randint() function.

Related posts

np.random.rand

np.random.permutation

np.random.randn

How to Get Random Numbers Between 0 and 1 in Python

Python random.seed()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.