MATLAB: Do the calls to the RAND function using an initial seed value not return the expected results

MATLAB

When I execute the following command:
rand('seed')
I receive the initial seed value: 1.0978e+009.
I then make several calls to RAND in the main workspace.
When I repeat the execution of the command:
rand('seed')
I receive the same seed value as previously: 1.0978e+009
The 'seed' value does not change, although it should, since I have changed the current seed through my calls to the RAND function.

Best Answer

This is the expected behavior because the above code is querying the state of the v4 generator, which uses the multiplicative congruential algorithm, but the current generator is not this generator. The uniformly distributed pseudorandom number generator function, RAND, maintains a "current" generator. This generator can be selected from the available algorithms in MATLAB (see related Technical Solution for descriptions of the available generators). Generating random numbers with RAND only changes the internal state of the current generator. The v4 generator has not been the default algorithm since MATLAB 4, so most likely the current generator is one of the other algorithms and the internal state of the v4 generator is not changing with each call to RAND.
If you would like to change the current generator used by RAND, you need to call RAND with two input arguments. For example, to select the Mersenne Twister algorithm (the preferred algorithm if it is available) use the following command
rand('twister', init)
where init is used to initialize the generator. init can be a scalar double precision number, which is sometimes called the seed, or init can be the output of
rand('twister')
The output of RAND, when called with one string input argument, is the internal state of the generator specified by the string argument (the form of the internal state is described in the documentation of RAND). If init is a scalar double precision number, i.e. a seed, this number is used to create the internal state. The ability to retrieve the value of the seed used to initialize the generators used by RAND is not available.
Note that calling RAND with two input argument accomplishes two things. One, it changes the current selected generating algorithm used by RAND. Two, it re-initializes the internal state of the generator. Whereas calling RAND with one string input argument only queries the internal state of the specified generator and does not change the current generator. Subsequent calls to RAND will use the same generator after querying the state of a specified generator as before the query.
All of the above description applies to MATLAB's normally distributed pseudorandom number generator function, RANDN, as well.
For more information about Random Numbers please see the documentation online for the current release of MATLAB:
Random Numbers: