File Exchange

image thumbnail

1-D random number generation with any analytical expression probability density function

version 1.4 (3.41 KB) by

Generate random numbers according to given analytical expression for the PDF



View License

Given an analytical expression for probability density distribution(PDF), one can use this code to generate a sample of 1-D random numbers. One can just run rand_generator to
function [random_vector] = rand_generator(myfun,xmin,xmax,number,mode_switch)

With a recent improvement, this code now can generate 100,000 samples within 1 second on my laptop.

Here, myfun is a the PDF, xmin and xmax is the range for the variable, while number is the total number for the sample. mode_switch can be set to either 'fast' or 'slow'. This is the only optional argument, with default value set to 'fast'.

The process create a array, then a distinctive calculate the PDF use the given function, following this, the cumulative density function is calculated. An automated check is done in case that your xmin and xmax are set to far and may duce waste of calculation time, or duce problem in worse case.

Then an interpretation is done, so that a random number from U[0,1] is mapped to the cdf, and so this sample will follow the distribution of the desired function. The interpretation process use linear or spline, depending on whether 'fast' or 'slow' is used.

I write this code with git, if you are interested, you can further develop this, or contact me if any bug found. I may further improve this code so that the input could be an array instead of a function, but I feel not that motivated, if anyone specify this requirement, I'll be more than happy to do this.

Comments and Ratings (8)

i want to ask you aboout tail and head why you set tail=tail-10 and head=head+10... the numbers 10 ie the number of bin ?

very good.. work


Tapiwa (view profile)

Hi Hu,
I recently submitted code that does something related to this. Check out ArbRand


Hu (view profile)

Hi ben,
Sorry that I can't reply soon enough. This issue might be due to the fact that your pdf spread quite narrow, and the code detect the probability to produce points beyond that region is lower than 1e-4, this is to make the code more efficient, however, you can modify line 68 and 73, to set this threshold smaller (however, the method I chose can't handle values smaller than eps.)
Y. Hu

Hi Hu,
i need to generate a sample of 1-D random numbers in [-10,10] which follows the PDF of Levy distribution. So i use your code. The problem that the resulting values are in the new range [-3.9394,3.9394] but i want that these values are in the original range including xmin=-10 and xmax=10.can you help me please?
thank you


Hu (view profile)

Hi ben,
Sorry for the late reply. You're right about the expression for Gaussian distribution, however, you can easily generate a Gaussian PDF random number from the built-in random number generator, while my file is design specifically for PDFs that don't have a built-in generator. So I wrote an example of PDF in case that you don't know how to use the code and just execute it without any input arguments. One property of my code is that it can normalise the PDF automatically, so you don't need to worry about the normalisation constant.
Y. Hu

hi Hu,
just a question about your code:
the analytical expression for probability density distribution(PDF)of gaussian distribution is:exp(-(x - m).^2 ./ (2*sigma^2)) ./ (sigma*sqrt(2*pi))where m is the average and sigma is the standard deviation.
but you didn't use this formula? can you explain me why?
thank you



update to generalize and make the code more robust.


In this update, I change the plot, so that it will scale according to the PDF function, instead of according to the histogram, which makes more sense.


Identify a bug, and changed it

MATLAB Release
MATLAB 7.12 (R2011a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video