Random Matrix and Date Generation Operation

  • Share this:

Code introduction


This code defines a Dagster operation (op) that generates a random matrix of specified size and a random date within a given date range. First, it generates a random matrix using the numpy library, then it generates a random date using the datetime library.


Technology Stack : numpy, datetime

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import random
from dagster import op
from datetime import datetime
import numpy as np

def generate_random_matrix(rows, cols):
    """
    Generates a random matrix of specified size using numpy.
    """
    return np.random.rand(rows, cols)

def random_date(start_date, end_date):
    """
    Generates a random datetime between two specified dates.
    """
    delta = end_date - start_date
    random_seconds = random.randrange(delta.total_seconds())
    return start_date + timedelta(seconds=random_seconds)

@op
def matrix_and_date_op(rows: int, cols: int, start_date: datetime, end_date: datetime):
    """
    This op generates a random matrix and a random date between two specified dates.
    """
    matrix = generate_random_matrix(rows, cols)
    random_date_value = random_date(start_date, end_date)
    return matrix, random_date_value                
              
Tags: