What does command 'rng default;' do?

341 views (last 30 days)
Shawn Miller
Shawn Miller on 23 Feb 2016
What is the difference with and without this command?

Answers (1)

jgg on 23 Feb 2016
Edited: jgg on 23 Feb 2016
When Matlab generates random numbers, they're not truly random; they are based on a pseudo-random number generating algorithm. The rng command controls the seed, or starting point, for this value. The "default" values resets it to the original value that MATLAB starts with; this evolves over time.
You can see this behaviour if you do something like called rand(10,1), then calling rng('default') then repeating the rand command. It will generate exactly the same "random" numbers.
This is generally useful for situations where you want to repeat an (ex ante) random outcome. For instance, in Monte Carlo simulation or in simulation-based optimization procedures.
Bachtiar Muhammad Lubis
Bachtiar Muhammad Lubis on 20 Dec 2018
so we should call those rng defaut right before the first rand and once again before the second rng default?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!