# What does Matlab's “randn” generate [closed]

I'm trying to read a Matlab script and can't figure out what does randn generate here:

a=0.1; b=a*randn;

Is it a random real number from (0, 1)?

-

## closed as too localized by Ｊ. Ｍ., Did, LVK, William, Asaf KaragilaAug 25 '12 at 0:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

randn stands for random normal. –  FrenzY DT. Aug 13 '12 at 11:18
The official doc says "RANDN with no arguments returns a scalar". How can you know from that what are the ranges?? –  Primož Kralj Aug 13 '12 at 11:32
The parameters deal with the size and number of elements you'd like to generate. If you would like $N(\mu,\sigma^2)$ random numbers then generate mu+sigma*randn(rows,columns). –  FrenzY DT. Aug 13 '12 at 11:35

Documentation from Matlab R2012a

### randn

Normally distributed pseudorandom numbers

### Syntax

r = randn(n)

It will generate a number $x$ such that $x$ follows $N(0,1^2)$

### My Box-Muller Implementation

#define SRAND()     srand(rand()%RAND_MAX*32768+rand()*rand()+500+(unsigned)time(0))
// continue to generate the same number if continuously used.

double uniform()
{
SRAND();
return (double)(rand()+32768*rand())/1073741824;
}
double gaussian()
{
return sqrt(-2.0*log(uniform()))*cos(2*PI*uniform());
}

-
So this means with "randn" I will get x € (-0.99 : 0.99)? –  Primož Kralj Aug 13 '12 at 11:33
@PrimožKralj Sorry but what you get is $x\in \mathbb R$. The number is gaussian, whose chances of appearance follows that of the probability density function (P.D.F) of a normal distribution. I.E. There's 99.7% chance that $x\in (-3,3)$ is true. –  FrenzY DT. Aug 13 '12 at 11:37
Jesus christ. I have no clue how to implement "randn" in C programing language. –  Primož Kralj Aug 13 '12 at 11:38
By Chebyshev, most of the numbers generated by a normal variate generator ought to fall within a few standard deviations from the mean... –  Ｊ. Ｍ. Aug 13 '12 at 11:41
Of course, one can avoid using trigonometric functions if need be... –  Ｊ. Ｍ. Aug 13 '12 at 11:48

Googling "Matlab randn" gives, as a first result, this Matlab documentation. The first example there says:

Example 1 Generate values from a normal distribution with mean 1 and standard deviation 2:

r = 1 + 2.*randn(100,1);

In your case then you are generating numbers with a mean of 0 and a standard deviation of 0.1, as you have b = 0 + 0.1 * randn(100,1).

In fairness, finding this through Google isn't hard.

-
I still don't understand - how would I write "randn" in C programming language?? –  Primož Kralj Aug 13 '12 at 11:30
"how would I write randn in C programming language??" - look up Box-Muller or ziggurat algorithms. –  Ｊ. Ｍ. Aug 13 '12 at 11:36
Basically it's generating a uniform random float, then using the Box Muller transformation to convert it into a random gaussian. –  FrenzY DT. Aug 13 '12 at 11:40
@Frenz, MATLAB is actually using the ziggurat algorithm of Marsaglia these days. See this. –  Ｊ. Ｍ. Aug 13 '12 at 11:42
@PrimožKralj the documentation page I linked to gives the answer (again). It says "r = randn returns a scalar." So calling just randn is the same as calling randn(1,1). You should really give the documentation page a read if you want to fully understand what's happening. –  James Fennell Aug 13 '12 at 13:00

From matlab:

help randn
RANDN Normally distributed random numbers.
R = RANDN(N) returns an N-by-N matrix containing pseudo-random values drawn from a normal distribution with mean zero and standard deviation one. RANDN(M,N) or RANDN([M,N]) returns an M-by-N matrix. RANDN(M,N,P,...) or RANDN([M,N,P,...]) returns an M-by-N-by-P-by-... array. RANDN with no arguments returns a scalar. RANDN(SIZE(A)) returns an array the same size as A.

-
In short, help is your friend. –  Ｊ. Ｍ. Aug 13 '12 at 11:21