matlabrandommontecarloprobability-densityuniform-distribution

Function in MATLAB to draw samples from Uniform Distribution


I want to draw randomly from a Uniform Distribution defined as below by code in MATLAB :-

pd1 = makedist('Uniform','lower',-0.0319,'upper',0.0319); % X1

In MATLAB the usual command is random() but the help file tells me its only for Guassian mixture distributions. So can it also be extended for use in Uniform Distribution or is there any other function to explicitly draw randomly for Monte Carlo Simulations.


Solution

  • For a uniform variable, you can use random as follows:

    lower_limit = -0.0319;
    upper_limit = 0.0319;
    sz = [1 10];
    x = random('unif', lower_limit, upper_limit, sz);
    

    But this is overly complicated, and slow: random analyzes the inputs and calls unifrnd, which in turn does some checks and finally calls rand. Instead, you can just use:

    lower_limit = -0.0319;
    upper_limit = 0.0319;
    sz = [1 10];
    x = lower_limit + (upper_limit-lower_limit)*rand(sz);
    

    In general, there are three levels of functions that can be used to create (pseudo)random numbers: