How to create random seed to have different results at each simevents run???
조회 수: 47 (최근 30일)
이전 댓글 표시
I would like to generate different seed at each time to have various outcomes ... I have tried "shuffle," however it showed that "it is not supported!" Wish someone can help me!Thank you~
My setting is as below: (this setting makes that my simulation is always with the same outcome every time.)
댓글 수: 0
채택된 답변
Teresa Hubscher-Younger
2017년 4월 13일
편집: Walter Roberson
2020년 7월 29일
Hi Min-Bin,
There are a couple of things you can try to vary the seed for each simulation run. It looks like you are running on Mac OS, and you can run the solution with fopen at the bottom of this response on Linux/Mac OS. On Windows, you may use the time to get the seed, which is unfortunately problematic for having multiple blocks generating random numbers.
By doing this, however, the results would be not be reproducible. We usually recommend creating a bunch of seeds at a time, caching them, and then reading from the cache, so that you can reproduce your results.
But here's a way that I hope will work for truly random results:
persistent rngInit;
if isempty(rngInit)
% only for Linux/Mac
fid = fopen('/dev/random');
rndval = fread(fid, 1, 'uint32')
fclose(fid);
seed = rndval(1);
rng(seed);
rngInit = true;
end
% Pattern: Exponential distribution
mu = 1;
dt = -mu * log(1 - rand());
-Teresa
댓글 수: 3
Sterling Baird
2020년 10월 24일
+1 for the seed cache suggestion. I'm guessing something like the following would work?
rng('shuffle')
nseeds = 100;
seedlist = randi(100000,nseeds,1);
Then for example save this list to a file and load the corresponding seed for each of the 100 runs.
Aaron Bramhasta
2024년 8월 27일
Hi Teresa,
Thank you for your solutions. I am currently facing similar problems as well on Windows.
I have tried other solutions and none have obtained a complete random numbers. May I ask what would you do if in my case? I am attaching my line of code from the event actions of an entity server where the rng is needed. The one commented out is also another method I tried.
% Generate the first random number between 0 and 100
rng1 = randi([0, 100]);
% Generate the second random number between 0 and 1
rng2 = randi([1, 2]);
%persistent rngInit;
%if isempty(rngInit)
% disp('Changed seed inside entity server to:');
% seed = rng1Seed;
% rng(seed);
% rngInit = true;
%end
% Pattern: Uniform distribution
% m: Minimum, M: Maximum
%m = 0; M = 100;
%rng1 = m + (M - m) * rand;
% Generate rng2 as a random number between 1 and 2
%rng2 = randi([1, 2]);
% Pattern: Uniform distribution
% m: Minimum, M: Maximum
%n = 1; N = 2;
%rng2 = n + (N - n) * rand;
% Determine the type of mission based on each Probability
if (rng1 > 10 && rng1 <= 100) && (rng2 == 1)
% General Purpose mission with load 1
entity.Part.Nj = 10; % [Cycles]
entity.Ops.FH = entity.Ops.FH + 2; % [hours]
entity.Ops.IntervalFH = entity.Ops.IntervalFH + 2; % [hours]
elseif (rng1 > 10 && rng1 <= 100) && (rng2 == 2)
% General Purpose mission with load 2
entity.Part.Nj = 100; % [Cycles]
entity.Ops.FH = entity.Ops.FH + 4; % [hours]
entity.Ops.IntervalFH = entity.Ops.IntervalFH + 4; % [hours]
elseif (rng1 <= 10) && (rng2 == 1)
% Combat Mission with load 1
entity.Part.Nj = 1080; % [Cycles]
entity.Ops.FH = entity.Ops.FH + 6; % [hours]
entity.Ops.IntervalFH = entity.Ops.IntervalFH + 6; % [hours]
elseif (rng1 <= 10) && (rng2 == 2)
% Combat Mission with load 2
entity.Part.Nj = 3600; % [Cycles]
entity.Ops.FH = entity.Ops.FH + 10; % [hours]
entity.Ops.IntervalFH = entity.Ops.IntervalFH + 10; % [hours]
end
Thank you so much in advance.
추가 답변 (2개)
Vijayalakshmi Chetlapalli
2018년 2월 23일
Adding to the suggestion by Min-bin, this is how it can be done. Open your simulink mdl, go to File-> Model Properties-> Model Properties->Callbacks. Define a variable in InitFcn that can be used as the seed for different iterations of the mdl, as shown here. This ensures that iterSeed is updated each time the mdl runs.
Then, you can use iterSeed with rng() in any of the blocks within your mdl to get different set of random numbers for each iteration. Example use in an Entity Generator block is shown below.
댓글 수: 4
Elliott Rachlin
2018년 10월 21일
I just noticed that I am getting "repeatability" that is not wanted. Every time I unload and reload Simulink into memory, the SAME random sequence begins again. I need the sequence to NOT restart across executions of Simulink. Is there a way based on time that I can generate non-repeating random number sequences?
Krishna Akella
2019년 5월 14일
편집: Krishna Akella
2019년 5월 14일
Hi,
All these methods will work most of the time. But sometimes there can be a partial overlap in the random numbers generated with two different seeds, putting a question mark on the conclusions of the simulation results.
A reliable way to ensure there is no overlap is to use random number streams instead. For more information, read the excellent blog posts by Loren Shure.
Regards,
Krishna
Abdolkarim Mohammadi
2019년 4월 7일
There is a block named 'Random Integer Number' or something like this that can produce different seed for your iterations even when fast restart is on. You can place this block in a simulink function and use it in entity generator as seed. Be aware that changing seed with InitFcn or random integer number block slows down your simulations.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete-Event Simulation에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!