Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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)?

share|improve this question
1  
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
add comment

closed as too localized by J. M., Did, LVK, William, Asaf Karagila Aug 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.

3 Answers

up vote 4 down vote accepted

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))
// Sorry about this strange looking SRAND() macro because otherwise it would 
// 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());
}
share|improve this answer
    
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... –  J. M. Aug 13 '12 at 11:41
1  
Of course, one can avoid using trigonometric functions if need be... –  J. M. Aug 13 '12 at 11:48
show 2 more comments

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.

share|improve this answer
4  
In short, help is your friend. –  J. M. Aug 13 '12 at 11:21
add comment

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.

share|improve this answer
    
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. –  J. M. 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
2  
@Frenz, MATLAB is actually using the ziggurat algorithm of Marsaglia these days. See this. –  J. M. Aug 13 '12 at 11:42
1  
@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
show 3 more comments

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