lognormal distribution - matlab

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Function to generate random numbers from a lognormal distribution
double lognormalRandom(double mean, double stddev) {
    double u1 = rand() / (double)RAND_MAX;
    double u2 = rand() / (double)RAND_MAX;

    double z = sqrt(-2.0  log(u1))  cos(2.0  M_PI  u2);
    return exp(mean + stddev * z);
}

int main() {
    // Parameters for the lognormal distribution
    double mean = 1.0; // Mean of the underlying normal distribution
    double stddev = 0.5; // Standard deviation of the underlying normal distribution
    int numSamples = 10; // Number of random samples to generate

    // Seed the random number generator
    srand((unsigned int)time(NULL));

    // Generate and print random numbers from the lognormal distribution
    printf("Generated Lognormal Random Numbers:\n");
    for (int i = 0; i < numSamples; ++i) {
        double randomValue = lognormalRandom(mean, stddev);
        printf("%f\n", randomValue);
    }

    return 0;
}