MATLAB: Random number generator errors after switching modes from ‘state’ to ‘shuffle’

MATLABrandrandnreinitializerngseedstatetwister

When I specify a seed value for the random number generator using:
randn('state', sd);
and then try to use the current time as the seed value using:
rng('shuffle');
MATLAB errors saying I need to reinitialize my random number generator: ERROR: Error using rng (line 99) The current random number generator is the legacy generator. This is because you have executed a command such as rand('state',0), which activates MATLAB's legacy random number behavior. You may not use RNG to reseed the legacy random number generator. Use rng('default') to reinitialize the random number generator to its startup configuration, or call RNG using a specific generator type, such as rng(seed,'twister').

Best Answer

Using "rand" or "randn" with 'seed', state', or 'twister' inputs is no longer a recommended syntax. "rng" is encouraged instead. "rand" or "randn" will still run, but will use the legacy generator. Once "rand" or "randn" is used, "rng" cannot be used to reseed without reinitializing. More information on the encouraged syntax can be found in this article: