Generate a random variable with an exponential distribution

15.1k views Asked by At

In Matlab using the rand routine, how should I write the code to generate 500 samples from an exponential distribution, whose pdf is:

(1/mu)*exp(-x/mu); x>=0
3

There are 3 answers

1
Luis Mendo On BEST ANSWER

Assuming you really have to do it using the rand function: exploit the property that the minus logarithm of a normalized uniform RV is a normalized exponential RV:

samples = -mu*log(rand(1,500));
0
horchler On

If you have the Statistic toolbox you can simply use exprnd much like you use rand:

r = exprnd(mu);

where the size of r will be the size of the mean, mu, or

r = exprnd(mu,m,n);

where mu is a scalar mean, and m and n are the size of your desired output. If you type edit exprnd, you'll see that the code is virtually identical to that kindly provided by @LuisMendo. You might find the other functions related to the exponential distribution helpful to, such as exppdf and expcdf. These are simple as well and implement basic equation that you can find in your textbook or on Wikipedia.

0
user263485 On

Use random function. For example to create a 4*6 matrix with mu=1.3 with an exponential distribution use:

random('Exponential',1.3,4,6)

or

random('exp',1.3,4,6)