Skip to content

rng

Initializing the built-in random number generator

rng(seed)

  • The input seed should be a non-negative integer.
  • It initializes the random number generator to a given initial state (determined by seed).
  • Since all pseudo-random number generating functions (e.g., randi, normrnd, exprnd, poissrnd) utilize the same generator, executing rng(seed) would simultaneously reset all of the functions to an initial state.

Example 1: When it is initialized with 17, rand() generates the same sequence of pseudo random numbers. When it is initialized with a different number, a different sequence is generated.

rng(17)
rand(1,5)
rng(17)
rand(1,5)
% Different seed number
rng(16)
rand(1,5)
Random number generator reset with seed 17.
ans =
0.015   0.405   0.883   0.924   0.268

Random number generator reset with seed 17.
ans =
0.015   0.405   0.883   0.924   0.268

Random number generator reset with seed 16.
ans =
0.263   0.964   0.869   0.010   0.366