I'm using Simevents and can't change the random seed in the Event Generator.

4 views (last 30 days)
I've added a iterSeed generator in the InitFcn* callback...
disp('Changed random seed to')
iterSeed = randi(400)
And in the Event Generator I want to test how dt = randi([1 3]) varies with different seeds to change to see how a different random string plays out....
I've tried all the suggested approachs below, but none seem to work.
persistent rngInit;
%if isempty(rngInit)
% disp('Changed seed inside Entity Gen to:')
% iterSeed
% rngInit = true;
%end
persistent rngInit;
if isempty(rngInit)
fid = fopen('/dev/random');
rndval = fread(fid,1,'uint32')
fclose(fid);
seed = rndval(1);
rng(seed);1
rngInit = true
end
dt = randi([1 3])
%persistent rngInit;
%if isempty(rngInit)
% rng(iterSeed);
% disp('Changed seed inside Entity Gen to:')
% iterSeed
% rngInit = true;
%end
%persistent rngInit;
%if isempty(rngInit)
% seed = 12345;
% rng(seed);
% rngInit = true;
%end

Answers (1)

Altaïr
Altaïr on 15 Apr 2025
Edited: Altaïr on 15 Apr 2025
A variable in the base workspace can be used to store the seed value and then applied within the Entity Generator. Here's how this can be done:
First, create a variable named 'seedVal' in the base workspace, and set it to the seconds value of the current system time with the following line:
seedVal=second(datetime())
Then, use these lines in the Intergeneration time action code field of the Entity Generator:
persistent rngInit;
if isempty(rngInit)
rng(seedVal);
rngInit = true;
end
dt = randi([1 3]);
This setup allows the intergeneration time to vary with simulation time as well as with the seed value.
For more detailed information on specifying intergeneration times, refer to this link: https://www.mathworks.com/help/releases/R2024a/simevents/ug/specifying-intergeneration-times-for-entities.html

Categories

Find more on Discrete-Event Simulation in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!