0001 function set_dynare_seed(a,b)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 global options_
0022
0023 if ~nargin
0024 error('set_dynare_seed:: I need at least one input argument!')
0025 end
0026
0027 matlab_random_streams = ~(exist('OCTAVE_VERSION') || matlab_ver_less_than('7.7') || options_.parallel_info.isHybridMatlabOctave);
0028
0029 if matlab_random_streams
0030 if nargin==1
0031 if ischar(a) && strcmpi(a,'default')
0032 options_.DynareRandomStreams.algo = 'mt19937ar';
0033 options_.DynareRandomStreams.seed = 0;
0034 s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
0035 if matlab_ver_less_than('7.12')
0036 reset(RandStream.setDefaultStream(s));
0037 else
0038 reset(RandStream.setGlobalStream(s));
0039 end
0040 return
0041 end
0042 if ischar(a) && strcmpi(a,'reset')
0043 s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
0044 if matlab_ver_less_than('7.12')
0045 reset(RandStream.setDefaultStream(s));
0046 else
0047 reset(RandStream.setGlobalStream(s));
0048 end
0049 return
0050 end
0051 if ischar(a)
0052 error('set_dynare_seed:: something is wrong in the calling sequence!')
0053 end
0054 if ~ischar(a)
0055 options_.DynareRandomStreams.algo = 'mt19937ar';
0056 options_.DynareRandomStreams.seed = a;
0057 s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
0058 if matlab_ver_less_than('7.12')
0059 reset(RandStream.setDefaultStream(s));
0060 else
0061 reset(RandStream.setGlobalStream(s));
0062 end
0063 return
0064 end
0065 elseif nargin==2
0066 if ~ischar(a) || ~( strcmpi(a,'mcg16807') || ...
0067 strcmpi(a,'mlfg6331_64') || ...
0068 strcmpi(a,'mrg32k3a') || ...
0069 strcmpi(a,'mt19937ar') || ...
0070 strcmpi(a,'shr3cong') || ...
0071 strcmpi(a,'swb2712') )
0072 disp('set_dynare_seed:: First argument must be string designing the uniform random number algorithm!')
0073 RandStream.list
0074 disp(' ')
0075 disp('set_dynare_seed:: Change the first input accordingly...')
0076 disp(' ')
0077 error(' ')
0078 end
0079 if ~isint(b)
0080 error('set_dynare_seed:: The second input argument must be an integer!')
0081 end
0082 options_.DynareRandomStreams.algo = a;
0083 options_.DynareRandomStreams.seed = b;
0084 s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
0085 if matlab_ver_less_than('7.12')
0086 reset(RandStream.setDefaultStream(s));
0087 else
0088 reset(RandStream.setGlobalStream(s));
0089 end
0090 end
0091 else
0092 if nargin==1
0093 if ischar(a) && strcmpi(a,'default')
0094 if exist('OCTAVE_VERSION') || matlab_ver_less_than('7.4')
0095 options_.DynareRandomStreams.algo = 'state';
0096 else
0097
0098 options_.DynareRandomStreams.algo = 'twister';
0099 end
0100 options_.DynareRandomStreams.seed = 0;
0101 rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
0102 randn('state',options_.DynareRandomStreams.seed);
0103 return
0104 end
0105 if ischar(a) && strcmpi(a,'reset')
0106 rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
0107 randn('state',options_.DynareRandomStreams.seed);
0108 return
0109 end
0110 if ~ischar(a) && isint(a)
0111 options_.DynareRandomStreams.seed = a;
0112 rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
0113 randn('state',options_.DynareRandomStreams.seed);
0114 else
0115 error('set_dynare_seed:: Something is wrong in the calling sequence!')
0116 end
0117 else
0118 error('set_dynare_seed:: Cannot use more than one input argument with your version of Matlab/Octave!')
0119 end
0120 end