MATLAB: How to generate reproducible normal random numbers using NORMRND in the Statistics Toolbox 6.1 (R2007b)

normrndresetseedStatistics and Machine Learning Toolbox

I would like to reset the seed of NORMRND so that I can restart the random number generation and receive the same random numbers I originally received.

Best Answer

MATLAB does not support a way of resetting the seed in NORMRND function. As a workaround, either create an array with NORMRND and reuse it or you can modify the 'normrnd.m' file. This is done by resetting the seed by saving 'normrnd.m' as 'normrndEDITED.m' and modifying the last lines to be,
persistent s
persistent i
if isempty(s)
s = randn('state');
end
if isempty(i)
i =0;
end
if i>10
randn('state',s);
i=0;
end
i=i+1;
r = randn(sizeOut) .* sigma + mu;
This will reset the seed after every 10 executions of the function.
Note THAT the attached file, normrndEDITED.m contains these modifications, however in order to use it successfully, you must place the normrndEDITED.m file in the same folder which has the original normrnd function.
Execute the following command in the MATLAB command line:
>> winopen([matlabroot '\toolbox\stats\stats\'])
This will open up the folder which contains the normrnd function in windows explorer. Place the edited function, normrndEDITED in this folder and execute the following command in the MATLAB command line to start using the new function:
>> rehash toolboxcache
The above command makes sure that all the toolbox files are update to date and useable by MATLAB.