File Exchange

image thumbnail


version (2.46 KB) by Pantelis Sopasakis
A simple and handy PDF (Probability Distribution Function) re-constructor and sampler.


Updated 09 May 2013

View License

PDFsampler is a MATLAB class that serves as a random number generator for custom probability distribution functions and is particularly useful for Monte Carlo simulations. Objects of this class are initialized using samples of your custom distribution of using histogram data of your PDF. It is well documented and pretty straight forward to use.

Cite As

Pantelis Sopasakis (2021). PDFsampler (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (6)

Hesam Aslan

Hi Pantelis,

Thank you for this great function. In the nextRandom part of your function, how can I modify it to generate n number of samples not just a single one.
e.g. r = pdfs.nextRandom(100,1) to create 100 rows of random samples from that distribution?



Minh Cao

Hi Pantelis,
Could you please tell me the name of the reconstruction method you are using? Perhaps a link to some research paper that references it?

Pantelis Sopasakis

@JonathanMayers: PDFSampler works with discrete distributions. You can pass the outputs of `hist` to its constructor.

Example #1:
random_numbers = [ ... ]; % some array
[v, p] = hist(random_numbers, 50);
sampler = PDFSampler(v, p);

#Example #2:
% In case you have an array of data
% You can pass it directly to the constructor:

In any case, you sample from the PDF using `sampler.nextRandom`

Jonathan Mayers

Thanks for this. I am not sure how to use the class for a custom pdf. Could you please provide an example of this?

Pantelis Sopasakis

An example of use can be found at

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!