For a recent project one of my research students needed to generate exponentially distributed random numbers in MATLAB. The Statistics Toolbox has a built-in function to do this, but I don’t have a license for this toolbox. Below is the code we wrote. It performs a log transformation to convert a uniformly distributed random number between zero and one (
rand(1)) to an exponentially distributed random number (
tau) drawn from an exponential distribution with mean
1/lambda. Note that in MATLAB, the
log function is the natural logarithm. Simple!
tau = -log(rand(1))/lambda;