Matlab creates same input values every time? Why?
5 views (last 30 days)
I'm creating processing times as follows:
function [ processing_times ] = PT( number_of_activity )
number_of_activity = 12;
max = 2500;
processing_times = randi([min max],1,number_of_activity)';
When I run this code, I obtain 12x1 matrix as follows:
After that, I quit the Matlab and open it again.
I run the same code and I obtain the same 12x1 matrix. This is possible? I use
code. This code includes randomness. How can I obtain the exactly same value when every time I close and reopen the Matlab. Shouldn't the Matlab give me different values every time I open and close it?
I use Matlab 2015a and I ran this code in 2023a but I encountered the same problem again.
Torsten on 26 Sep 2023
Edited: Torsten on 26 Sep 2023
Shouldn't the Matlab give me different values every time I open and close it?
No. But you get different values if you call the function several times in one run.
at the start of your code should also give you different random numbers for different runs:
But usually this is not desirable because the results from one run should be reproducable.
More Answers (3)
Karl Cronburg on 26 Sep 2023
Edited: Karl Cronburg on 26 Sep 2023
Walter Roberson on 26 Sep 2023
Every time you load matlab, it does the equivalent of rng('default') which initializes the seed to 0.
It is expected that the random number order is the same starting from the beginning of each MATLAB session.
See rng for more information on controlling the random number generator.
Caution: although you can use rng('shuffle') to initialize the seed based on the current time, doing so is not considered secure. Do not use it for producing seeds for cryptographic communications, or for producing seeds for drawing lottery numbers. Creating good seeds for secure purposes is hard