Matlab creates same input values every time? Why?

조회 수: 2 (최근 30일)
Abdullah Türk
Abdullah Türk 2023년 9월 26일
댓글: Abdullah Türk 2023년 9월 26일
Hi everybody,
I'm creating processing times as follows:
function [ processing_times ] = PT( number_of_activity )
number_of_activity = 12;
min= 100;
max = 2500;
processing_times = randi([min max],1,number_of_activity)';
end
When I run this code, I obtain 12x1 matrix as follows:
1481
1372
760
696
1184
646
2031
2467
172
1386
309
2025
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
randi([min max],1,number_of_activity)'
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
Torsten 2023년 9월 26일
편집: Torsten 2023년 9월 26일
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.
Selecting
rng("shuffle")
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.
  댓글 수: 3
Torsten
Torsten 2023년 9월 26일
편집: Torsten 2023년 9월 26일
How and where should I use rng("shuffle") in my code?
As first line in your main script.
Abdullah Türk
Abdullah Türk 2023년 9월 26일
Torsten, I positioned rng('shuffle') at first line in my code as follows and the problem has solved. Thank you.
clc;
clear;
rng('shuffle')
% numWorkers = 2;
% pool = parpool('local', numWorkers, 'IdleTimeout', Inf);
activities = [12 16 20];
pactivities = [5 7 9];
workers = [200 250 300];
skills = [5 10 15];
I learned a new information about Matlab. I've been trying for hours for problem and rng('shuffle') solved it. Thanks again :)

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Karl Cronburg
Karl Cronburg 2023년 9월 26일
편집: Karl Cronburg 2023년 9월 26일
You can get a random seed based on the current operating system time with the rng function:
rng("shuffle");
  댓글 수: 1
Abdullah Türk
Abdullah Türk 2023년 9월 26일
Karl Cronburg, when every time I open Matlab every time, "how does it give me same processeing times" I said myself. But, it has a logical explanation, thanks for your contribution for the problem.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2023년 9월 26일
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

Steven Lord
Steven Lord 2023년 9월 26일
Others have told you how to get the numbers to not repeat after startup. But to answer your "Why?" question, this is expected behavior. See this documentation page that explains why this happens.
  댓글 수: 1
Abdullah Türk
Abdullah Türk 2023년 9월 26일
"Why Do Random Numbers Repeat After Startup?"
Steven Lord, thank you. I read the the article. There are very useful information in it. Thanks.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by