time | Calls | line |
---|
| | 1 | function settings = rng(arg1,arg2)
|
| | 2 | %RNG Control the random number generator used by RAND, RANDI, and RANDN.
|
| | 3 | % RNG(SD) seeds the random number generator using the non-negative
|
| | 4 | % integer SD so that RAND, RANDI, and RANDN produce a predictable
|
| | 5 | % sequence of numbers.
|
| | 6 | %
|
| | 7 | % RNG('shuffle') seeds the random number generator based on the current
|
| | 8 | % time so that RAND, RANDI, and RANDN produce a different sequence of
|
| | 9 | % numbers after each time you call RNG.
|
| | 10 | %
|
| | 11 | % RNG(SD,GENERATOR) and RNG('shuffle',GENERATOR) additionally specify the
|
| | 12 | % type of the random number generator used by RAND, RANDI, and RANDN.
|
| | 13 | % GENERATOR is one of:
|
| | 14 | %
|
| | 15 | % Generator Description
|
| | 16 | % ------------------------------------------------------------------
|
| | 17 | % 'twister' Mersenne Twister
|
| | 18 | % 'simdTwister' SIMD-oriented Fast Mersenne Twister
|
| | 19 | % 'combRecursive' Combined Multiple Recursive
|
| | 20 | % 'multFibonacci' Multiplicative Lagged Fibonacci
|
| | 21 | % 'philox' Philox generator with 10 rounds
|
| | 22 | % 'threefry' Threefry generator with 20 rounds
|
| | 23 | % 'v5uniform' Legacy MATLAB 5.0 uniform generator
|
| | 24 | % 'v5normal' Legacy MATLAB 5.0 normal generator
|
| | 25 | % 'v4' Legacy MATLAB 4.0 generator
|
| | 26 | %
|
| | 27 | % RNG('default') puts the settings of the random number generator used by
|
| | 28 | % RAND, RANDI, and RANDN to their default values so that they produce the
|
| | 29 | % same random numbers as if you restarted MATLAB. In this release, the
|
| | 30 | % default settings are the Mersenne Twister with seed 0.
|
| | 31 | %
|
| | 32 | % SCURR = RNG returns the current settings of the random number generator
|
| | 33 | % used by RAND, RANDI, and RANDN. The settings are returned in a
|
| | 34 | % structure SCURR with fields 'Type', 'Seed', and 'State'.
|
| | 35 | %
|
| | 36 | % RNG(S) restores the settings of the random number generator used by
|
| | 37 | % RAND, RANDI, and RANDN back to the values captured previously by
|
| | 38 | % S = RNG.
|
| | 39 | %
|
| | 40 | % SPREV = RNG(...) returns the previous settings of the random number
|
| | 41 | % generator used by RAND, RANDI, and RANDN before changing the settings.
|
| | 42 | %
|
| | 43 | % Example 1: Retrieve and Restore Generator Settings
|
| | 44 | % s = rng % get the current generator settings
|
| | 45 | % x = rand(1,5) % RAND generates some values
|
| | 46 | % rng(s) % restore the generator settings
|
| | 47 | % y = rand(1,5) % generate the same values so x and y are equal
|
| | 48 | %
|
| | 49 | % Example 2: Restore Settings for Legacy Generator
|
| | 50 | % prevS = rng(0,'v5uniform') % use legacy generator, save previous settings
|
| | 51 | % x = rand % legacy startup value .9501
|
| | 52 | % rng(prevS) % restore the previous settings
|
| | 53 | %
|
| | 54 | % See <a href="matlab:helpview([docroot '\techdoc\math\math.map'],'update_random_number_generator')">Replace Discouraged Syntaxes of rand and randn</a> to use RNG to replace
|
| | 55 | % RAND or RANDN with the 'seed', 'state', or 'twister' inputs.
|
| | 56 | %
|
| | 57 | % See also RAND, RANDI, RANDN, RandStream, NOW.
|
| | 58 |
|
| | 59 |
|
| | 60 | % See <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/math/brn4ixh.html#brvku_2">Choosing a Random Number Generator</a> for details on these generators.
|
| | 61 |
|
| | 62 | % Copyright 2010-2018 The MathWorks, Inc.
|
| | 63 |
|
< 0.001 | 1 | 64 | if nargin == 0 || nargout > 0
|
| | 65 | % With no inputs, settings will be returned even when there's no outputs
|
| | 66 | s = RandStream.getGlobalStream();
|
| | 67 | if strcmpi(s.Type,'legacy')
|
| | 68 | settings = struct('Type','Legacy','Seed',legacySeedStr(),'State',{s.State});
|
| | 69 | else
|
| | 70 | settings = struct('Type',RandStream.compatName(s.Type),'Seed',s.Seed,'State',{s.State});
|
| | 71 | end
|
< 0.001 | 1 | 72 | end
|
| | 73 |
|
< 0.001 | 1 | 74 | if nargin > 0
|
< 0.001 | 1 | 75 | arg1 = convertStringsToChars(arg1);
|
< 0.001 | 1 | 76 | if isstruct(arg1) && isscalar(arg1)
|
| | 77 | inSettings = arg1;
|
| | 78 | if nargin > 1
|
| | 79 | error(message('MATLAB:rng:maxrhs'));
|
| | 80 | elseif ~isempty(setxor(fieldnames(inSettings),{'Type','Seed','State'}))
|
| | 81 | throw(badSettingsException);
|
| | 82 | end
|
| | 83 | if strcmpi(inSettings.Type,'legacy')
|
| | 84 | handleLegacyStruct(inSettings); % restores the legacy stream state
|
| | 85 | else
|
| | 86 | % Create a new stream as specified, then set its state
|
| | 87 | try
|
| | 88 | s = RandStream(inSettings.Type,'Seed',inSettings.Seed);
|
| | 89 | s.State = inSettings.State;
|
| | 90 | catch me
|
| | 91 | throw(badSettingsException);
|
| | 92 | end
|
| | 93 | RandStream.setGlobalStream(s);
|
| | 94 | end
|
< 0.001 | 1 | 95 | else
|
< 0.001 | 1 | 96 | if isnumeric(arg1) && isscalar(arg1) % rng(seed) or rng(seed,gentype)
|
< 0.001 | 1 | 97 | seed = arg1;
|
< 0.001 | 1 | 98 | if nargin == 1
|
0.167 | 1 | 99 | gentype = getCurrentType;
|
| | 100 | else
|
| | 101 | arg2 = convertStringsToChars(arg2);
|
| | 102 | gentype = arg2;
|
| | 103 | if ~ischar(gentype)
|
| | 104 | error(message('MATLAB:rng:badSecondOpt'));
|
| | 105 | elseif strcmpi(gentype,'legacy')
|
| | 106 | errorLegacyGenType;
|
| | 107 | end
|
< 0.001 | 1 | 108 | end
|
| | 109 | elseif ischar(arg1)
|
| | 110 | if strcmpi(arg1,'shuffle') % rng('shuffle') or rng('shuffle',gentype)
|
| | 111 | seed = RandStream.shuffleSeed;
|
| | 112 | if nargin == 1
|
| | 113 | gentype = getCurrentType;
|
| | 114 | else
|
| | 115 | arg2 = convertStringsToChars(arg2);
|
| | 116 | gentype = arg2;
|
| | 117 | if ~ischar(gentype)
|
| | 118 | error(message('MATLAB:rng:badSecondOpt'));
|
| | 119 | elseif strcmpi(gentype,'legacy')
|
| | 120 | errorLegacyGenType;
|
| | 121 | end
|
| | 122 | end
|
| | 123 | elseif strcmpi(arg1,'default') % rng('default')
|
| | 124 | if nargin > 1
|
| | 125 | error(message('MATLAB:rng:maxrhs'));
|
| | 126 | end
|
| | 127 | seed = 0;
|
| | 128 | gentype = RandStream.DefaultStartupType;
|
| | 129 | else % possibly rng(gentype) or rng(gentype,seed)
|
| | 130 | error(message('MATLAB:rng:badFirstOpt'));
|
| | 131 | end
|
| | 132 | else
|
| | 133 | error(message('MATLAB:rng:badFirstOpt'));
|
< 0.001 | 1 | 134 | end
|
| | 135 |
|
| | 136 | % Create a new stream using the specified seed
|
< 0.001 | 1 | 137 | try
|
0.004 | 1 | 138 | s = RandStream(gentype,'Seed',seed); % RandStream handles the compatibility names
|
| | 139 | catch me
|
| | 140 | if strcmp(me.identifier,'MATLAB:RandStream:create:UnknownRNGType')
|
| | 141 | error(message('MATLAB:rng:unknownRNGType',gentype));
|
| | 142 | elseif strcmp(me.identifier,'MATLAB:RandStream:BadSeed')
|
| | 143 | error(message('MATLAB:rng:badSeed'));
|
| | 144 | else
|
| | 145 | throw(me);
|
| | 146 | end
|
< 0.001 | 1 | 147 | end
|
0.002 | 1 | 148 | RandStream.setGlobalStream(s);
|
< 0.001 | 1 | 149 | end
|
< 0.001 | 1 | 150 | end
|
Other subfunctions in this file are not included in this listing.