MATLAB: How to generate random numbers from a censured Weibull distribution in Statistics Toolbox 7.2 (R2009b)

Statistics and Machine Learning Toolbox

I would like to generate random numbers from a Weibull distribution that as a upper bound, 'ub' and is renormalized in the domain [0,ub].

Best Answer

The RAND/WBLRND functions in the Statistics Toolbox are not customizable to generic distribution functions. However, in the case of the Weibull distribution, one can analytically derive the renormalized PDF, corresponding CDF and hence a random number generator using the inverse relationship in terms of a Uniformly distributed random number.
Let's say 'ub' is the upper bound on the Weibull distribution, then the normalization factor is given by
scaleFactor = 1 - exp( (-ub/A) * 1/B );
where A and B are the parameters of a regular Weibull distribution.
Following this, and using the inverse relationsip, a random number from a upper bounded Weibull Distribution is given as follows
r = A .* (-log(1 - rand(sizeOut) .* scaleFactor)) .^ (1./B);
where 'sizeOut' is the number of random numbers to be generated.
One can create a custom function my modifying the file wblrnd.m and including the above two lines to generate the new values.
Related Question