%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Demonstration of the usage of setting the "state" of the random number
% generator, so that the sequence of pseudo-random numbers returned
% by rand can be repeated.
% 
% Run this script more than one time to verify that the second 
% set of pseudo-random numbers generated is the same each time the 
% script is executed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% First generate a sequence of 10 pseudo-random numbers which should 
% differ from run to run.

fprintf('''True'' random numbers:\n\n');
for i = 1 : 10
   rand
end

disp('')
input('Enter anything to continue: ');

% Now set the state of the random number generator by calling rand 
% with the string 'state' as the first argument, and some arbitrary
% vector of length <= 625 as the second.  Provided that the *same* 
% vector is used to set the state of the generator, the same sequence
% of pseudo-random numbers will be generated subsequently.

% Here we will simply use vectors of length 1 as the second argument.

% Generate first reproducible set of pseudo-random numbers: supply 
% length-1 vector '[0]' as second argument to rand.
rand('state', [0]);

fprintf('\nReproducible random numbers (set 1):\n\n');
for i = 1 : 10
   rand
end

disp('')
input('Enter anything to continue: ');

% Generate second reproducible set of pseudo-random numbers: supply 
% length-1 vector '[1]' as second argument to rand.
rand('state', [1]);

fprintf('\nReproducible random numbers (set 2):\n\n');
for i = 1 : 10
   rand
end

disp('')
input('Enter anything to continue: ');
