time | Calls | line |
---|
| 1 | 451 | function s = RandStream(type, varargin)
|
| | 452 | %RANDSTREAM Create a random number stream.
|
| | 453 | % S = RandStream('GENTYPE') creates a random number stream that uses the
|
| | 454 | % uniform pseudorandom number generator algorithm specified by GENTYPE.
|
| | 455 | % Type "RandStream.list" for a list of possible values for GENTYPE, or
|
| | 456 | % see <a href="matlab:helpview([docroot '\techdoc\math\math.map'],'choose_random_number_generator')">Choosing a Random Number Generator</a> for details on these generator
|
| | 457 | % algorithms.
|
| | 458 | %
|
| | 459 | % One you have created a random stream, you can use RANDSTREAM.setGlobalStream
|
| | 460 | % to make it the global stream, so that RAND, RANDI, and RANDN draw values
|
| | 461 | % from it.
|
| | 462 | %
|
| | 463 | % [ ... ] = RandStream('GENTYPE', 'PARAM1',val1, 'PARAM2',val2, ...) allows
|
| | 464 | % you to specify optional parameter name/value pairs to control creation of
|
| | 465 | % the stream. Parameters are:
|
| | 466 | %
|
| | 467 | % Seed - a non-negative scalar integer seed with which to
|
| | 468 | % initialize the stream, or 'shuffle' to create a seed
|
| | 469 | % based on the current time. Default is 0.
|
| | 470 | % NormalTransform - the transformation algorithm that RANDN(S, ...) uses
|
| | 471 | % to generate normal pseudorandom values from uniform
|
| | 472 | % pseudorandom values. The property value is one of
|
| | 473 | % 'Ziggurat' (the default), 'Polar', or 'Inversion'.
|
| | 474 | %
|
| | 475 | % Streams created using RandStream may not be independent from each other.
|
| | 476 | % Use RandStream.CREATE to create multiple streams that are independent.
|
| | 477 | %
|
| | 478 | % Examples:
|
| | 479 | %
|
| | 480 | % Create a random number stream, make it the global stream, and save and
|
| | 481 | % restore its state to reproduce the output of RANDN:
|
| | 482 | % s = RandStream('mrg32k3a');
|
| | 483 | % RandStream.setGlobalStream(s);
|
| | 484 | % savedState = s.State;
|
| | 485 | % z1 = randn(1,5)
|
| | 486 | % s.State = savedState;
|
| | 487 | % z2 = randn(1,5) % z2 contains exactly the same values as z1
|
| | 488 | %
|
| | 489 | % Return RAND, RANDI, and RANDN to their default startup settings:
|
| | 490 | % s = RandStream('mt19937ar','Seed',0)
|
| | 491 | % RandStream.setGlobalStream(s);
|
| | 492 | %
|
| | 493 | % Replace the current global random number stream with a stream whose
|
| | 494 | % seed is based on the current time, so RAND, RANDI, and RANDN will
|
| | 495 | % return different values in different MATLAB sessions. NOTE: It is
|
| | 496 | % usually not desirable to do this more than once per MATLAB session.
|
| | 497 | % s = RandStream('mt19937ar','Seed','shuffle');
|
| | 498 | % RandStream.setGlobalStream(s);
|
| | 499 | %
|
| | 500 | % See also RANDSTREAM, RANDSTREAM.CREATE, RANDSTREAM.LIST, RNG,
|
| | 501 | % RANDSTREAM.GETGLOBALSTREAM, RANDSTREAM.SETGLOBALSTREAM,
|
| | 502 | % RANDSTREAM/RAND, RANDSTREAM/RANDI, RANDSTREAM/RANDN.
|
| | 503 |
|
< 0.001 | 1 | 504 | if nargin < 1
|
| | 505 | error(message('MATLAB:RandStream:TooFewInputs'));
|
| | 506 | end
|
< 0.001 | 1 | 507 | type = convertStringsToChars(type);
|
| | 508 |
|
< 0.001 | 1 | 509 | pnames = {'seed' 'normaltransform' 'parameters'};
|
| 1 | 510 | dflts = { 0 [] [] };
|
0.002 | 1 | 511 | [seed,randnalg,params] = getargs(pnames, dflts, varargin{:});
|
| | 512 |
|
< 0.001 | 1 | 513 | if ~ischar(type)
|
| | 514 | error(message('MATLAB:RandStream:InvalidRNGType'));
|
| | 515 | end
|
| 1 | 516 | if ischar(seed) && isequal(lower(seed),'shuffle')
|
| | 517 | seed = RandStream.shuffleSeed;
|
< 0.001 | 1 | 518 | elseif ~isnumeric(seed) || ~isreal(seed) ||~isscalar(seed) || ~(0<=seed && seed<2^32)
|
| | 519 | % Allow non-integer seed so that sum(100*clock) works. Will truncate below.
|
| | 520 | error(message('MATLAB:RandStream:BadSeed'));
|
| | 521 | end
|
| | 522 |
|
< 0.001 | 1 | 523 | if isempty(params)
|
| | 524 | % none given, it will be defaulted to zero below
|
| | 525 | elseif ~isnumeric(params) || ~isreal(params) || ~isvector(params) ...
|
| | 526 | || ~all(params == round(params)) || ~all(params >= 0)
|
| | 527 | error(message('MATLAB:RandStream:BadParams'));
|
| | 528 | elseif any(strcmpi(type,RandStream.BuiltinTypes))
|
| | 529 | error(message('MATLAB:RandStream:ParamNotValid', type));
|
| | 530 | end
|
| | 531 |
|
0.001 | 1 | 532 | s.Type = RandStream.algName(type(:)');
|
< 0.001 | 1 | 533 | s.Seed = uint32(floor(seed)); % truncate if not integer
|
< 0.001 | 1 | 534 | s.Params = uint64(params); % possibly empty
|
< 0.001 | 1 | 535 | s.NumStreams = uint64(1);
|
< 0.001 | 1 | 536 | s.StreamIndex = uint64(1); % stored one-based
|
< 0.001 | 1 | 537 | s.SpawnIncr = uint64(1);
|
< 0.001 | 1 | 538 | s.StreamID = builtin('_RandStream_create_mex',s.Type,s.NumStreams,s.StreamIndex,s.Seed,s.Params);
|
| 1 | 539 | if ~isempty(randnalg), set(s,'NormalTransform',randnalg); end
|
< 0.001 | 1 | 540 | end
|
Other subfunctions in this file are not included in this listing.