Why does MATLAB generate identical random values after re-opening the program?

1 Ansicht (letzte 30 Tage)
I run the code below
y = rand(1,5)
and I get the following values:
0.8147 0.9058 0.1270 0.9134 0.6324
I close MATLAB and reopen it again and enter the same code and I get the same random values. Why does MATLAB create the exact same numbers each time?
  1 Kommentar
Stephen23
Stephen23 am 24 Jul. 2018
Bearbeitet: Stephen23 am 24 Jul. 2018
"Why does MATLAB create the exact same numbers each time?"
Because:
  • computers can't really generate random numbers.
  • that is exactly what MATLAB is specified to do:

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rightia Rollmann
Rightia Rollmann am 24 Jul. 2018
I set rng to 'default', so I expected that it'll generate new random numbers after re-running MATLAB, but that is not the case. Why? How can I get MATLAB to generate new random numbers after re-opening it?
  2 Kommentare
Guillaume
Guillaume am 24 Jul. 2018
Follow the link in Stephen's comment to get the answer to your question, or do what Madhan told you.
Also, don't use the answer box for comments.
Stephen23
Stephen23 am 25 Jul. 2018
Bearbeitet: Stephen23 am 25 Jul. 2018
"I set rng to 'default', so I expected that it'll generate new random numbers after re-running MATLAB..."
The rng documentation explains that the 'default' option as "This way, the same random numbers are produced as if you restarted MATLAB. The default settings are the Mersenne Twister with seed 0." Clearly with the same seed it will generate exactly the same values each time. That is what it is specified to do!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

madhan ravi
madhan ravi am 24 Jul. 2018
Bearbeitet: madhan ravi am 24 Jul. 2018
hi, see the link below:
https://www.mathworks.com/help/matlab/ref/rng.html

Kategorien

Mehr zu Random Number Generation finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by