View License

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

» Watch video

Highlights from

4.8 | 58 ratings Rate this file 98 Downloads (last 30 days) File Size: 38.1 KB File ID: #7309 Version: 1.4
image thumbnail



Alex Bar-Guy (view profile)


29 Mar 2005 (Updated )

Efficient Random Variates Generator (from over 50 distributions).

| Watch this File

File Information


EFFICIENT RANDOM VARIATES GENERATOR (from over 50 distributions):

Chi-Square (Non-Central),
Chi-Square (Central),
F (Central),
F (Non-Central),
Generalized Inverse Gaussian,
Generalized Hyperbolic,
Hyperbolic Secant,
Inverse Gaussian,
Negative Binomial,
Normal-Inverse-Gaussian (NIG),
Truncated Normal,
Uniform (continuous),
Von Mises,
Wigner Semicircle,


This file inspired Dynamic Copula Toolbox Version 1 and Compound Poisson Simulation.

MATLAB release MATLAB 6.5.1 (R13SP1)
Other requirements -
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (71)
02 Nov 2016 jabbar Hussien

Extensive work! Thank you.

Comment only
18 Oct 2016 jmlarson

There appears to be an issue with the beta distribution:

>> histogram(randraw('beta',[1, 2],1e5));

has a very different appearance than it should:

Comment only
09 Sep 2016 seongjin

Extensive work! Thank you!

08 Sep 2016 Nuno Jorge

30 Mar 2014 Abdulaziz

how can I get doc of Generalized Inverse Gaussian
I would see the density. I typed doc randraw(Generalized Inverse Gaussian) but no thing appear.

Comment only
27 Jan 2014 Nima

Nima (view profile)

You saved me!!! many thanks!

16 May 2013 Long Vo

Thank you very much !

Comment only
15 May 2013 Reza Farrahi Moghaddam

01 May 2013 Zhenmao Wan

06 Mar 2013 Nicolas

Gorgeous. Many Thanks

23 Nov 2012 Pete

Pete (view profile)

Impressive. Many thanks

20 Aug 2012 Peter Nave

Extremely useful!

01 Aug 2012 Katha

Katha (view profile)

very good work. thank you for sharing with us.

I have a question:
Who could I plot a diagram like this one you show?

Comment only
19 Jun 2012 Dylan Muir

Dylan Muir (view profile)

04 Jun 2012 Ali montazeri

Great help. Thank you very much for the neat code.

30 Dec 2011 Philipp Andres

Burr Dist: pdf should be:
pdf(y) = c*d * y.^(-c-1) .* (1+y.^c).^(-d-1)
(minus sign in front of power of c)

Comment only
23 Nov 2011 Wes Devauld

09 Sep 2011 Mohammad Al-Jarrah

Dear Sir, thank you for your code. I am interesting in alpha-mu distribution. Can you help me to generate this this random variable according to this distribution.

Comment only
16 Aug 2011 Zack

Zack (view profile)

16 Aug 2011 Zack

Zack (view profile)


Comment only
29 Jul 2011 Andreas Papadakis

excellent work

21 Oct 2010 Xavier Xavier

11 Oct 2010 TideMan

27 Aug 2010 Radi Hristov

Using - 'randraw('bessel', [2 0.9], [1 1e5])' I get an error message:
??? Attempted to access EE(7.46288e+008); index out of bounds because numel(EE)=1.

Error in ==> randraw>randFrom5Tbls at 4373
out(ii) = EE(floor(jj(ii)-t4) + 1);

Error in ==> randraw at 807
out = randFrom5Tbls( P, offset, sampleSize);

Error in ==> DG_IS_gamrnd_BS_Probe at 100
n0 = randraw('bessel', [v z], 1);

What is the Problem with the Bessel Generator?

23 Jul 2010 Rainer

Rainer (view profile)

15 Nov 2009 Md Mahmudul Hasan

Hmm... really helped a lot.!

Comment only
06 Oct 2009 Erdal Bizkevelci

10 Sep 2009 Dominic Vella

Generally very useful. Agree that this ought to be standard in Matlab w/o toolboxes.

However, there seems to be a bug in 'beta'. When I have a=1, b>1 I get a kink in the cdf at x=0.5 and it does not follow betainc as expected.

As soon as I have a>1, even a=1.000001, this kink goes away and the cdf matches betainc perfectly. Any ideas what's going on?

17 May 2009 Palash Ranjan


15 May 2009 Peter Harris

Thanks, this is very useful.
A minor bug in the beta distribution code - the last line in the case statement is
reshape( out, SampleSizeIn );
but it needs
out = reshape( out, SampleSizeIn );
to work.

Comment only
17 Apr 2009 Arun M

Arun M (view profile)

I'm not sure if anyone has pointed it out, but binomial seems to have a bug:
You define switchflag within a if-else clause in line 881, but it needs to be defined outside it, since, after the clause, you write:
Line 935: if switchFlag
Did old versions of Matlab support use of variables outside what's meant to be their scope?

Comment only
05 Dec 2008 koubaa kamal

koubaa kamal (view profile)

this is an excellent work.

05 Nov 2008 Mason Yost

17 Sep 2008 C Schwalm

This is excellent, and is yet another FEX submission that should be part of standard Matlab sans toolboxes.

31 May 2008 ren sisi

01 May 2008 yj won

28 Mar 2008 Saaaaa sdfasdf

28 Feb 2008 paolo mott

and the multivariate case?

Comment only
30 Nov 2007 Lingzhi Liao

It's a wonderful job

02 Nov 2007 Kyoto Univ

30 Sep 2007 Seno Jinca

03 Sep 2007 Jack Lee

Great program for various distribution generation.

01 Jul 2007 Zhang Jianlong

Your program helps me very much in GIG random generator! thank you! I do a progrom by myself,but its efficiency is poor.

26 Jun 2007 Sunil S

incluing Nakagami-m distribution would have made it perfect

18 May 2007 Munsik Park


13 Apr 2007 Aasmund Kvamme

Very good, but the help-text keeps rolling off my screen. Perhaps some sort of printable documentation?

06 Apr 2007 on muk


27 Mar 2007 Daniel Strezov

It works excellent for me :)

16 Mar 2007 Ged Ridgway

Hi, with regard to Xudong Wang's request for Rician samples, you are welcome to take the (very simple) code from my toolbox and adapt it to randraw.


Comment only
10 Mar 2007 Rashmi S.

13 Feb 2007 Andre Crawford

This code is great! The HELP is also wonderfully organized.

26 Dec 2006 Xudong Wang

If it had included Ricean,Nakagami-m distributions, this program would be perfect.

29 Nov 2006 Yanwei Wang

thanks a lot for your excellent work!

01 Oct 2006 Peter Nave

Many thanks for this truly excellent code. In particular, the organization of HELP is remarkable.

14 Sep 2006 tien le


24 Aug 2006 AnyƟ yamoun

Nice work !
Fast operations .
No more needs for many Libraries .

13 Aug 2006 Jaime Jaime

very useful.Thanks!

30 Jun 2006 Jenny Brown

prevent me from switching to SAS.

15 Jun 2006 Valentin Boca


22 May 2006 Tim Brown

Absolutely Fantastic - you are a star!

17 Mar 2006 Maria Afonso

Thanks for sharing it with us!

19 Dec 2005 Andreas Wiik

Great worl, thanks for sharing this useful script.

09 Dec 2005 Subramaniam Lakshmanan

y = randraw('uniform', [2, 3], 1, 1e5);

I am getting the following error message if i execute the above statement. But if i am not specifying any parameter a & b, it works fine. Can u help me with it.

??? Undefined function or variable 'true'.

Error in ==> D:\Random\randraw.m (validateParam)
On line 4105 ==> condLogical = true;

Error in ==> D:\Random\randraw.m
On line 3751 ==> validateParam(funcName, 'Uniform', 'uniform', '[a, b]', 'b-a', b-a, {'> 0'});

30 Nov 2005 srikanth vadde

17 Nov 2005 Rv Le Borgne

Very, very useful!!!

06 Oct 2005 Omer Kravi

Excellent work!!! Thanks for GIG random variables generator !!!

01 Sep 2005 Earl Lawrence

Most of this program seems pretty good. There's a bit of bug in the gamma code. The reference they cite has a different algorithm for alpha < 1 which is omitted in the code causing trouble, particularly if try and set alpha = 1/3.

26 Jul 2005 dmirty larin

Great and very useful work!

17 May 2005 c m

If I had confidence in the program it would be great.

I tried the program with poisson distribution help, i.e. randraw( 'po') but it produced an error message. See below.

Maybe the problem is similar to the binomial
distribution problem noted in the 4/14/05

This is a great piece of work, but unless
there is confidence in the accuracy of the
program it is not very useful.

Here is the problem I encountered.
randraw( 'po')

The output is an error:
??? if h<=1 &&
Missing variable or function.
Syntax error...
On line 1799

15 May 2005 Feng Sun

Great work! Thank you very much for sharing this with us!

27 Apr 2005 Anubhav Swami

Comprehensive random variable generator. Excellent work.

14 Apr 2005

Bug fix: Generation from binomial distribution has worked by 'binomial' usage string, but the help for this distribution has worked by 'binom'

18 May 2005

Bugs fix:
1) Poisson:t instead of ti (lam< 21.4)
2) GIG: support to chi=0 or psi=0 cases
3) Beta: column sampleSize
4) Cauchy: typo in example
5) Chi: typo in example
6) Non-central F: number of input params
7) IG: typo in example

22 Jul 2005

Bug fix:
Typo in GIG distribution generation:
should be 'out' instead of 'x' in lines 1852 and 1858

06 Sep 2005

Bugs fix:
 1) GAMMA distribution:
    -special case for a<1
 2) GIG distribution:
    -typo in help;
    -code adjustment to overcome
     possible computational overflows;
3) CHI SQUARE distribution
    -typo in help

12 Dec 2005

true' and 'false' functions were replased by ones and zeros to support Matlab releases below 6.5

12 Dec 2005

true' and 'false' functions were replased by ones and zeros to support Matlab releases below 6.5

12 Dec 2005

true' and 'false' functions were replased by ones and zeros to support Matlab releases below 6.5

21 Feb 2007

GIG distribution (thanks to Mr. Demetris Lamnisos) bug fix:
Computational exceptions in the reparameterized GIG generation were fixed

21 Nov 2012 1.1

Added BSD License;
New distributions are planned to be added to the generator in the near future;

06 Mar 2013 1.4

Updated comments

Contact us