Skip to content

rng

general · scripts/general/rng.m

  rng (seed)
  rng (seed, generator)
  rng ("default")
  rng ("shuffle")
  rng ("shuffle", generator)
  rng (s)
  s = rng
  s = rng (...)
Control the random number generators behind rand, randn,
randi, randperm and related functions.
rng (seed) seeds the generators with the nonnegative
integer seed (0 up to, not including, 2^32), so that later random
numbers are reproducible.  A fractional seed is truncated, as in
MATLAB.  rng ("default") is the same as rng (0).
rng ("shuffle") seeds from the clock.  The generator is the
Mersenne twister ("twister" / "mt19937ar").  The uniform generator is seeded
in the same way as the reference Mersenne twister initialisation, so
rand yields the well known sequence for a given seed.
With an output argument the settings in effect before the call are
returned as a structure with the fields Type, Seed and
State.  Passing such a structure to rng restores the
generators exactly.  State is a 625-by-1 uint32 column,
the same layout MATLAB uses for the Mersenne twister: 624 state words
and a position word.  A previously saved 3125-word Nexus state (five
generator blocks) is still accepted.
See also: rand, randn, randi, randperm.