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-2014 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-2014 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 = ~(isoctave || 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) || (ischar(a) && strcmpi(a, 'clock'))
0052             options_.DynareRandomStreams.algo = 'mt19937ar';
0053             if ischar(a)
0054                 options_.DynareRandomStreams.seed = rem(floor(now*24*60*60), 2^32);
0055             else
0056                 options_.DynareRandomStreams.seed = a;
0057             end
0058             s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
0059             if matlab_ver_less_than('7.12')
0060                 reset(RandStream.setDefaultStream(s));
0061             else
0062                 reset(RandStream.setGlobalStream(s));
0063             end
0064             return
0065         end
0066         error('set_dynare_seed:: something is wrong in the calling sequence!')
0067     elseif nargin==2
0068         if ~ischar(a) || ~( strcmpi(a,'mcg16807') || ...
0069                             strcmpi(a,'mlfg6331_64') || ...
0070                             strcmpi(a,'mrg32k3a') || ...
0071                             strcmpi(a,'mt19937ar') || ...
0072                             strcmpi(a,'shr3cong') || ...
0073                             strcmpi(a,'swb2712') )
0074             disp('set_dynare_seed:: First argument must be string designing the uniform random number algorithm!')
0075             RandStream.list
0076             skipline()
0077             disp('set_dynare_seed:: Change the first input accordingly...')
0078             skipline()
0079             error(' ')
0080         end
0081         if ~isint(b)
0082             error('set_dynare_seed:: The second input argument must be an integer!')
0083         end
0084         options_.DynareRandomStreams.algo = a;
0085         options_.DynareRandomStreams.seed = b;
0086         s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
0087         if matlab_ver_less_than('7.12')
0088             reset(RandStream.setDefaultStream(s));
0089         else
0090             reset(RandStream.setGlobalStream(s));
0091         end
0092     end
0093 else% Use old matlab interface.
0094     if nargin==1
0095         if ischar(a) && strcmpi(a,'default')
0096             if isoctave
0097                 options_.DynareRandomStreams.algo = 'state';
0098             else
0099                 options_.DynareRandomStreams.algo = 'twister';
0100             end
0101             options_.DynareRandomStreams.seed = 0;
0102             rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
0103             randn('state',options_.DynareRandomStreams.seed);
0104             return
0105         end
0106         if ischar(a) && strcmpi(a,'reset')
0107             rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
0108             randn('state',options_.DynareRandomStreams.seed);
0109             return
0110         end
0111         if (~ischar(a) && isint(a)) || (ischar(a) && strcmpi(a,'clock'))
0112             if ischar(a)
0113                 options_.DynareRandomStreams.seed = floor(now*24*60*60);
0114             else
0115                 options_.DynareRandomStreams.seed = a;
0116             end
0117             rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
0118             randn('state',options_.DynareRandomStreams.seed);
0119             return
0120         end
0121         error('set_dynare_seed:: Something is wrong in the calling sequence!')
0122     else
0123         error('set_dynare_seed:: Cannot use more than one input argument with your version of Matlab/Octave!')
0124     end
0125 end

Generated on Fri 21-Nov-2014 23:58:41 by m2html © 2005