NumPy matlib.rand() Function:
The Matlib.rand() function of the NumPy module returns a matrix of random values with a specified shape.
It creates a matrix of the given shape and propagates it with random samples from a uniform distribution over a range[0, 1).
Syntax:
numpy.matlib.rand(*args)
Parameters
*args: This is required. It is the shape of the output. Each integer specifies the size of one dimension If given as N integers. If given as a tuple, this tuple gives the complete/entire shape.
Return Value:
A matrix of random values with the shape specified by *args is returned.
- Python NumPy random.random() Function
- Python NumPy random.random_sample() Function
- Python NumPy power() Function
NumPy matlib.rand() Function in Python
Example1
Approach:
- Import numpy module using the import keyword.
- Import matlib function of numpy module using the import keyword
- Pass the shape of the matrix(as a tuple)as an argument to the matlib.rand() function of numpy module to create a matrix of the given shape with random values from a uniform distribution over the range[0, 1).
- Store it in a variable.
- Print the above-obtained matrix with random values of the given shape.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Import matlib function of numpy module using the import keyword import numpy.matlib # Pass the shape of the matrix(as a tuple)as an argument to the matlib.rand() # function of numpy module to create a matrix of the given shape # with random values from a uniform distribution over the range[0, 1). # Store it in a variable. gvn_matrx = np.matlib.rand(2,2) # Print the above obtained matrix with random values of the given shape print("The above obtained matrix with random values of the given shape:") print(gvn_matrx)
Output:
The above obtained matrix with random values of the given shape: [[0.66416079 0.89898958] [0.44260668 0.2464688 ]]
Example2
The Other arguments are ignored when the first argument is a tuple.
Approach:
- Import numpy module using the import keyword.
- Import matlib function of numpy module using the import keyword
- Pass the shape of the matrix(as a tuple), and other arguments to the matlib.rand() function of numpy module to create a matrix of the given shape with random values from a uniform distribution over the range[0, 1). Store it in a variable.
- The Other arguments are ignored when the first argument is a tuple.
- Print the above-obtained matrix with random values of the given shape.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Import matlib function of numpy module using the import keyword import numpy.matlib # Pass the shape of the matrix(as a tuple), and other arguments to the matlib.rand() # function of numpy module to create a matrix of the given shape # with random values from a uniform distribution over the range[0, 1). # Store it in a variable. # The Other arguments are ignored when the first argument is a tuple. gvn_matrx = np.matlib.rand((2,2), 3) # Print the above obtained matrix with random values of the given shape print("The above obtained matrix with random values of the given shape:") print(gvn_matrx)
Output:
The above obtained matrix with random values of the given shape: [[0.82772574 0.9736207 ] [0.24653178 0.1373968 ]]