Home > src > setSeed.m

# setSeed

## PURPOSE

- function for setting the seed for the random number generator

## SYNOPSIS

function setSeed(seed)

## DESCRIPTION

     - function for setting the seed for the random number generator

     copyright 2009-2012 Blair Armstrong, Christine Watson, David Plaut

        This file is part of SOS

        SOS is free software: you can redistribute it and/or modify
        it for academic and non-commercial purposes
        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.  For commercial or for-profit
        uses, please contact the authors (sos@cnbc.cmu.edu).

        SOS 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.

## CROSS-REFERENCE INFORMATION

This function calls:

- verbosePrint - master print function that regulates cmd line output based on verbosity flags

This function is called by:

## SOURCE CODE

    0001 % - function for setting the seed for the random number generator
    0002 %
    0003 % copyright 2009-2012 Blair Armstrong, Christine Watson, David Plaut
    0004 %
    0005 %    This file is part of SOS
    0006 %
    0007 %    SOS is free software: you can redistribute it and/or modify
    0008 %    it for academic and non-commercial purposes
    0009 %    under the terms of the GNU General Public License as published by
    0010 %    the Free Software Foundation, either version 3 of the License, or
    0011 %    (at your option) any later version.  For commercial or for-profit
    0012 %    uses, please contact the authors (sos@cnbc.cmu.edu).
    0013 %
    0014 %    SOS 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 SOS (see COPYING.txt).
    0021 %    If not, see <http://www.gnu.org/licenses/>.
    0022
    0023
    0024 function setSeed(seed)
    0025     %% Sets the seed for the random number generator.
    0026     % Useful for repeating the exact same optimization in the future, or to
    0027     % force a different series of random numbers to be used in running the
    0028     % same optimization.  Supply a positive number to set a fixed seed.
    0029     %
    0030     % Alternatively, supplying a negative number will set the generator to
    0031     % a different random state on initialization, ensuring that a different
    0032     % set of random states is generated.
    0033     %
    0034     %PARAMETERS:
    0035     % seed - must be a positive number for that particular value to be used
    0036     %       to seed the random generator.  Negative numbers set the random
    0037     %       number generator to novel random states not linked to those
    0038     %       numbers and can be used to forcibly generate different random
    0039     %       sequences each time the algorithm is run.
    0040     %
    0041
    0042     p = inputParser;
    0043     p.addRequired('seed',@(seed)validateattributes(seed, {'numeric'}, ...
    0044                 {'scalar', 'integer'}));
    0045     p.parse(seed);
    0046
    0047     if seed < 0
    0048         verbosePrint('Seed < 0; setting to random value based on clock time',...
    0049             'setSeed_set');
    0050         seed = sum(100*clock);
    0051     else
    0052         verbosePrint(['Setting seed to: ',num2str(seed)],...
    0053             'setSeed_set');
    0054     end
    0055
    0056         rand('twister',seed);
    0057 end