Why do I get correlated random numbers when I invoke RANDN after incrementing the seed by a small number in MATLAB 7.5 (R2007b)?

1 view (last 30 days)
When I execute the following code
test = zeros(1,1000);
for n=1:1000
randn('state', n);
test(n) = randn;
end
I notice that the "random" elements in the variable "test" are correlated with each other beyond what I would expect from a random sampling of the normal distribution.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
Successively incrementing the seed for RANDN and then invoking RANDN will produce highly correlated outputs and, for this reason, is not the suggested use for RANDN. The correct way to use RANDN is to initialize the state of RANDN outside the FOR loop:
test = zeros(1,1000);
randn('state', 0);
for n=1:1000
test(n) = randn;
end
Once this is done, invoking the RANDN function inside the loop will generate uncorrelated random numbers in successive iterations. This will also create reproducible results from one execution of code to the next.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!