I am a newbie in stat. I am completing my thesis in Evolutionary algorithm. I have to generate some random numbers from T-distribution or Laplace distribution. How can I do this?

An easy and simple explanation would be appreciated.

  • 1
    $\begingroup$ @crucified Any statistical package in mind? $\endgroup$ – chl May 21 '11 at 18:58
  • 5
    $\begingroup$ @cruc Matlab has an inverse t distribution: see mathworks.com/help/toolbox/stats/tinv.html . All you do is apply this function to a uniform random variate in the range (0,1). $\endgroup$ – whuber May 21 '11 at 19:37
  • 2
    $\begingroup$ As tinv is part of the Statistics toolbox, you'd have to pay extra for this, unless you are eligible for a student version, in which case it comes as part of the package. $\endgroup$ – cardinal May 21 '11 at 22:13
  • 5
    $\begingroup$ Quick and dirty: sqrt(n)*randn(1)/norm(randn(n,1)) will generate a $t$-distributed variate with $n$ degrees of freedom. This is probably the fastest to code, but certainly not the fastest to execute, especially if you need a very large number of them. $\endgroup$ – cardinal May 22 '11 at 2:19
  • 3
    $\begingroup$ Not quite sure why you bothered to ask about Laplace distribution, as it explicitly tells you how to generate these in the link you give in your question! $\endgroup$ – probabilityislogic May 23 '11 at 8:22

Here's how to do this in Matlab using TINV from that statistics toolbox:

%# choose the degree of freedom
df = 4; %# note you can also choose an array of df's if necessary

%# create a vector of 100,000 uniformly distributed random varibles
uni = rand(100000,1);

%# look up the corresponding t-values
out = tinv(uni,df);

With a more recent version of Matlab, you can also simply use TRND to create the random numbers directly.

out = trnd(100000,df);

Here's the histogram of out enter image description here

EDIT Re:merged question

Matlab has no built-in function for drawing numbers from a Laplace distribution. However, there is the function LAPRND from the Matlab File Exchange that provides a well-written implementation.


Easy answer: Use R and get n variables for a $t$-distribution with df degrees of freedom by rt(n, df). If you don't use R, maybe you can write what language you use, and others may be able to tell precisely what to do.

If you don't use R or another language with a built in random number generator for the $t$-distribution, but you have access to the quantile function, $Q$, for the $t$-distribution and you can generate a uniform random variable $U$ on $[0,1]$ then $Q(U)$ follows a $t$-distribution.

Else take a look at this brief section in the Wikipedia page.

  • $\begingroup$ Please forgive my ignorance. I want to do it in matlab. I may have to use t-distribution with degrees of freedom ranging from 1 to 30. In wiki, formula for only 1, 2, 4 degrees of freedom is given. Is there any generalized formula for quantile function? Could you give formulas for other even number of degrees of freedom? $\endgroup$ – user May 21 '11 at 19:23
  • 2
    $\begingroup$ @crucified, no closed form will exist for the $t$ quantile function. See the G. W. Hill references listed here. You could also get the source code for GSL and look up the appropriate functions. I haven't looked myself, but it's probably easy enough to port to MATLAB. $\endgroup$ – cardinal May 21 '11 at 22:55
  • $\begingroup$ @cardinal +1, especially given the fact that there exist GSL bindings for Octave. $\endgroup$ – chl May 22 '11 at 10:31

By looking at the Wikipedia article, I've written a function to generate random variables from the Laplace dsistribution. Here it is:

function x = laplacernd(mu,b,sz)
%LAPLACERND Generate Laplacian random variables 
%  x = LAPLACERND(mu,b,sz) generates random variables from a Laplace
%  distribution having parameters mu and b. sz stands for the size of the
%  returned random variables. See [1] for Laplace distribution.
%  [1] http://en.wikipedia.org/wiki/Laplace_distribution
%  by Ismail Ari, 2011

if nargin < 1 % Equal to exponential distribution scaled by 1/2
    mu = 0;
if nargin < 2
    b = 1;
if nargin < 3
    sz = 1;

u = rand(sz) - 0.5;
x = mu - b*sign(u) .* log(1-2*abs(u));

And here is a code snippet to use it

clc, clear
mu = 30;
b = 2;
sz = [50000 1];

x = laplacernd(mu,b,sz);

Laplace distribution


The best (fastest to run, not fastest to code;) free solution I have found in Matlab was to wrap R's MATHLIB_STANDALONE c library with a mex function. This gives you access to R's t-distribution PRNG. One advantage of this approach is that you also can use the same trick to get variates from a non-central t distribution.

The second best free solution was to use octave's implementation of trnd. Porting from octave turned out to be more work than wrapping c code for me.

For my tastes, using uniform generation via rand and inverting via tinv was much too slow. YMMV.


You can use the same approach that was described in response to your question about generating random numbers from a t-distribution. First generate uniformly distributed random numbers from (0,1) and then apply the inverse cumulative distribution function of the Laplace distribution, which is given in the Wikipedia article you linked to.

  • $\begingroup$ NB: This is a reply to a second question that was subsequently merged with the present one. As @mark999 correctly points out here, the two questions are the same except one asks for Laplace variates and the original (this one) asks for Student t variates. $\endgroup$ – whuber May 22 '11 at 15:37
  • 1
    $\begingroup$ @whuber: it might make sense, then, to edit this question to include the Laplace distribution. $\endgroup$ – Jonas May 22 '11 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.