Generate a random variable with an exponential distribution

user3115854 picture user3115854 · Dec 18, 2013 · Viewed 14.7k times · Source

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

Answer

Luis Mendo picture Luis Mendo · Dec 18, 2013

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));