Random Matrix Generator

  • Share this:

Code introduction


This function generates a random matrix with specified number of rows and columns, with the default data type being float64.


Technology Stack : NumPy

Code Type : The type of code

Code Difficulty :


                
                    
import numpy as np

def random_matrix(rows, cols, dtype=np.float64):
    """
    Generate a random matrix with specified number of rows and columns.

    Parameters:
    rows (int): Number of rows in the matrix.
    cols (int): Number of columns in the matrix.
    dtype (np.dtype): Data type of the matrix elements, default is float64.

    Returns:
    np.ndarray: A random matrix of specified size and data type.
    """
    return np.random.rand(rows, cols).astype(dtype)                
              
Tags: