Code can be made faster by vectorizing the while loop, and also to return multiple draws from the distribution. Just make sure to validate your modifications with a non-symmetric distribution like a beta.
This method is called CDF inversion - Generate a standard uniform random number and apply the inverse CDF of the distribution D to that number to obtain a new random number drawn from distribution D. This is available in MATLAB for a number of densities: eg: expinv, binoinv etc..
http://www.mathworks.com/access/helpdesk/help/toolbox/stats/bqttfc1.html#bqucf5l-2
Valuable code to have.
Code can be made faster by vectorizing the while loop, and also to return multiple draws from the distribution. Just make sure to validate your modifications with a non-symmetric distribution like a beta.
This technique of random number generation is discussed here: http://en.wikipedia.org/wiki/Inverse_transform_sampling
This method is called CDF inversion - Generate a standard uniform random number and apply the inverse CDF of the distribution D to that number to obtain a new random number drawn from distribution D. This is available in MATLAB for a number of densities: eg: expinv, binoinv etc..
http://www.mathworks.com/access/helpdesk/help/toolbox/stats/bqttfc1.html#bqucf5l-2
Comment only