Function in MATLAB to draw samples from Uniform Distribution

750 views Asked by At

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.

1

There are 1 answers

3
Luis Mendo On BEST ANSWER

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:

  • random is a sort of common interface for generating random numbers. Based on the specified inputs it calls the appropriate function, such as unifrnd for a uniform distribution, exprnd for an exponential distribution, ...
  • There are specific functions for each distribution, typically called "···rnd": unifrnd, exprnd, normrnd, ... Internally, these call some of the functions to be described next, and apply some transformation to achieve the desired distribution.
  • The lowest-level (built-in), fastest functions are: rand for a continuous uniform distribution on (0,1), randn for a normal (Gaussian) distribution with mean 0 and variance 1, and randi for a uniform discrete distribution.