View License

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

» Watch video

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

4.0 | 2 ratings Rate this file 7 Downloads (last 30 days) File Size: 3.41 KB File ID: #40598 Version: 1.4
image thumbnail

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


Hu (view profile)


01 Mar 2013 (Updated )

Generate random numbers according to given analytical expression for the PDF

| Watch this File

File Information

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.

Required Products MATLAB
MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
21 Jan 2017 george veropoulos

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 ?

Comment only
20 Oct 2016 george veropoulos

very good.. work

Comment only
06 Aug 2014 Tapiwa

Tapiwa (view profile)

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

24 Jul 2014 Hu

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

Comment only
09 Jun 2014 ben chattah thoraya

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

Comment only
17 Dec 2013 Hu

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

Comment only
09 Nov 2013 ben chattah thoraya

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

Comment only
23 Jul 2013 Riccardo Buscicchio

24 Jul 2013 1.1

Identify a bug, and changed it

29 Oct 2013 1.3

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.

06 Aug 2014 1.4

update to generalize and make the code more robust.

Contact us