MATLAB: Rand(‘seed’,1)

randomrandom number generator

hi there, can anyone explain me the different between using 'rand('seed',1)' inside the loop and outside the loop? Assuming I generate some random number inside the loop.
Ta,Rak

Best Answer

Seeding the random number generator means initializing it to a certain status. Seeding inside the loop means, that all "random" numbers created inside the loop will be the same in each iteration:
for i = 1:3
rand('seed', 1);
disp(rand);
end
Result: 0.51291 0.51291 0.51291
This is not very useful. Seeding RAND outside the loop allows you to reproduce the results:
for j = 1:2
rand('seed', 1);
for i = 1:3
disp(rand);
end
end
Now you get two series of 3 different numbers:
0.51291 0.46048 0.3504 0.51291 0.46048 0.3504