Tuesday, October 2, 2007

Matlab - randn

randn(n) : randomly produce an nxn matrix
using ... method of generator

randn(m,n) : randomly produce an mxn matrix
using ... method of generator

s = randn(method) returns in s the current internal state of the generator selected by method.
It does not change the generator being used.
'state' Uses Marsaglia's ziggurat algorithm (the default in MATLAB versions 5 and later).
The period is approximately 2^64.
'seed' Uses the polar algorithm (the default in MATLAB version 4).
The period is approximately (2^31-1)*(pi/8).
randn(method, n) initializes the state of this generator using the value of 100
Different initializers give different result.
Same initializer gives same result.

So, basically, if we want to keep the same "random" set of number through several calculation, we can do it by storing internal state.
Note that after each time we call randn, after the function generalize some random numbers, its internal state changes. If we want it to reproduce some set of "random" numbers again, we need to keep track of the internal state.

randn('state', 0) Set randn to its default initial state
randn('state', sum(100*clock)) Initialize randn to a different state each time

Example:
s = randn('state') % Save the current state
u1 = randn(3) % Generate 3x3 values
randn('state', s) % reset the state
u2 = randn(3) % contains exactly the same values as u1


No comments: