How to create random seed to have different results at each simevents run???
37 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Min-Bin Lin
il 8 Apr 2017
Commentato: Sterling Baird
il 24 Ott 2020
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.)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/162750/image.png)
0 Commenti
Risposta accettata
Teresa Hubscher-Younger
il 13 Apr 2017
Modificato: Walter Roberson
il 29 Lug 2020
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
2 Commenti
Sterling Baird
il 24 Ott 2020
+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.
Più risposte (2)
Vijayalakshmi Chetlapalli
il 23 Feb 2018
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/179293/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/179294/image.png)
4 Commenti
Elliott Rachlin
il 21 Ott 2018
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
il 14 Mag 2019
Modificato: Krishna Akella
il 14 Mag 2019
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
il 7 Apr 2019
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 Commenti
Vedere anche
Categorie
Scopri di più su Simulink Functions in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!