Home > matlab > set_dynare_seed.m

set_dynare_seed

PURPOSE ^

Set seeds depending on matlab (octave) version. This routine is called in dynare_config and can be called by the

SYNOPSIS ^

function set_dynare_seed(a,b)

DESCRIPTION ^

 Set seeds depending on matlab (octave) version. This routine is called in dynare_config and can be called by the 
 user in the mod file.
    
 Copyright (C) 2010-2012 Dynare Team

 This file is part of Dynare.

 Dynare is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Dynare is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function set_dynare_seed(a,b)
0002 % Set seeds depending on matlab (octave) version. This routine is called in dynare_config and can be called by the
0003 % user in the mod file.
0004 %
0005 % Copyright (C) 2010-2012 Dynare Team
0006 %
0007 % This file is part of Dynare.
0008 %
0009 % Dynare is free software: you can redistribute it and/or modify
0010 % it under the terms of the GNU General Public License as published by
0011 % the Free Software Foundation, either version 3 of the License, or
0012 % (at your option) any later version.
0013 %
0014 % Dynare is distributed in the hope that it will be useful,
0015 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 % GNU General Public License for more details.
0018 %
0019 % You should have received a copy of the GNU General Public License
0020 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
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% Use new matlab interface.
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% Use old matlab interface.
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                 % Twister was introduced in MATLAB 7.4
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

Generated on Mon 20-May-2013 23:36:15 by m2html © 2005