Facebook Page Twitter Page LinkedIn Page
× NumPy Tutorial NumPy Resources


The NumPy matlib.rand() function is used to return a matrix of random values with given shape. The function creates a matrix of the given shape and propagate it with random samples from a uniform distribution over [0, 1).

Syntax

numpy.matlib.rand(*args)

Parameters

*args Required. Specify shape of the output. If given as N integers, each integer specifies the size of one dimension. If given as a tuple, this tuple gives the complete shape.

Return Value

Returns a matrix of random values with shape given by *args.

Example: defining shape using integers

In the below example, matlib.rand() function is used to create a matrix of given shape containing random values from a uniform distribution over [0, 1).

import numpy as np
import numpy.matlib

mat = np.matlib.rand(3,2)
print(mat)

The possible output of the above code could be:

[[0.76220569 0.45832152]
 [0.2573741  0.16884502]
 [0.67076371 0.94206513]]

Example: defining shape using tuple

When the first argument is a tuple, other arguments are ignored. Consider the following example.

import numpy as np
import numpy.matlib

mat = np.matlib.rand((3,2),3)
print(mat)

The possible output of the above code could be:

[[0.30831529 0.91864008]
 [0.23476181 0.53646912]
 [0.67201204 0.86197789]]

❮ NumPy - Functions