what is the use of seed.

21 views (last 30 days)
SANJEEV KUMAR
SANJEEV KUMAR on 27 Nov 2011
(1)
for k=1:3
rand('seed',k);
a=rand(1,5)
end
or (2)
for k=1:3
rand(k);
a=rand(1,5)
end
what is the difference between above two command.

Answers (1)

Wayne King
Wayne King on 27 Nov 2011
(1) is seeding the random number generator so that it produces a predictable sequence of "random" numbers.
For example, if you run the following loop again and again
for k = 1:3
rand('seed',k);
a = rand(5,1),
end
you will get the same 3 vectors for the variable, a.
In (2), rand(k) is producing a kxk matrix of uniform random numbers, then producing a 1x5 vector of uniform random numbers. So I'm not sure what the point of (2) is.
  3 Comments
Walter Roberson
Walter Roberson on 27 Nov 2011
Seeding in a loop is seldom desirable, and can often reduce randomness, and is not an acceptable mechanism for security. There have been successful attacks against systems that used seeding in a loop to try to implement security.
Seeding _before_ a loop can be useful.
Daniel Shub
Daniel Shub on 28 Nov 2011
The 'seed' method is pretty dated at this point.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!