Signup/Sign In

NumPy rand() function

In this tutorial, we will cover numpy.matlib.rand() function of the Numpy library.

The numpy.matlib.rand() function is used to generate a matrix where all the entries are initialized with some random values.

The numpy.matlib is a matrix library used to configure matrices instead of ndarray objects.

This function helps to create a matrix of a given shape and filled with random value in the range [0, 1). Hence, we can use this method to create a matrix of random values with a given shape.

Syntax of matlib.rand():

The required syntax to use this function is as follows:

numpy.matlib.rand(*args)

Note: In the above syntax *args is representing the arguments and it indicates the shape of the output. If *args are given as N integers, then each integer specifies the size of one dimension. If *args is given as a tuple then this tuple gives the complete shape.

Returned Values:

This method will return a matrix of random values with shape given by *args.

Basic Example for matlib.rand():

Given below is a basic example of how to create a random matrix using the above mentioned method:

import numpy as np  
import numpy.matlib  
  
x = numpy.matlib.rand(4,3)
print("The Random Matrix is :")
print(x)

The output of the above code:

Numpy rand() function output example

Example 2:

In the below example we will use one argument in the form of a tuple and it is important to note here that if one argument is a tuple then other arguments are ignored by this method:

import numpy as np
import numpy.matlib

x = numpy.matlib.rand((5, 6), 4)
print(x)

The output of the above code:

Numpy rand() function output example

Summary

In this tutorial we learned about numpy.rand() mathematical function of the Numpy library. We also covered its syntax, parameters as well as the value returned by this function along with a code example.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.