How can I pass in an rng(seed) into a call to sim(...), as below (SimEvents model):

14 visualizaciones (últimos 30 días)
The script I generate is like this. The rng() call seems to be ignored within the call to sim().
rng(5);
sim(xxx);
rng(6);
sim(xxx);

Respuestas (5)

Elliott Rachlin
Elliott Rachlin el 11 de Dic. de 2018
I don't think that Krishna's answer is correct. I was able to do what I wanted after learning that I needed to add the following additional statement: val=1;
into this sequence (taken from the "MATLAB action" section of the "Integration time action" box in an Entity Generator block):
coder.extrinsic('rand');
m=0;
M=1;
val=1;
val=rand;
x1=m+(M-m)*val;
dt=x1;

Krishna Akella
Krishna Akella el 11 de Dic. de 2018
Hi Elliot,
It seems like the random number generator is unique to each block and the default seed is 0. Setting rng(5) on the command line sets the seed for the MATLAB workspace. So the seed has to be set separately for each block where a random number generator is used (which is what the 'insert pattern' button does in the event action).
- Krishna

Krishna Akella
Krishna Akella el 11 de Dic. de 2018
Hi Elliot,
I am glad you were able to solve your problem.
I am trying to understand your solution and I tried adding the code you mentioned into an entity generator's 'Intergeneration time action' with a few additional lines to check if the rng seed changes:
coder.extrinsic('rand');
coder.extrinsic('num2str');
m=0;
M=1;
val=1;
val=rand;
x1=m+(M-m)*val;
dt=x1;
r=rng;
disp(['seed: ' num2str(r.Seed)]);
When I try to set the rng seed from the MATLAB command prompt, I see the following output:
>> rng(5)
>> sim(gcs)
seed: 0
seed: 0
seed: 0
>> rng(6)
>> sim(gcs)
seed: 0
seed: 0
>>
I have attached my model (which is a simple generator -> terminator). What do you think I am missing?
Thanks,
Krishna

Krishna Akella
Krishna Akella el 20 de Dic. de 2018
Hi Elliot,
You are correct! Your solution works correctly!
I have identified what was happening and here is what is happening:
All block event actions are converted to 'C' code, which are then compiled in memory. And this compiled code is invoked to perform the event action. This is done to speed up the simulation. By adding a call to "coder.extrinsic('rand')" you are specifying that the MATLAB action method should call the interpreted MATLAB method rand, instead of the code-generated method. Therefore the seeds are now in sync with what is set on the MATLAB command prompt. And the reason you need to initialize val is because, to generate the 'C' code, the return type of the call to the external rand function cannot be inferred. By setting val = 1 (or any double value), you are letting the code generation know the type of val (as double).
I have identitied the problem in the code I have posted above, which was the source of my confusion. I added the following in the block:
coder.extrinsic('rand');
coder.extrinsic('num2str');
m=0;
M=1;
val=1;
val=rand;
x1=m+(M-m)*val;
dt=x1;
r=rng;
disp(['seed: ' num2str(r.Seed)]);
But missed adding coder.extrinsic('rng').
- Krishna

Krishna Akella
Krishna Akella el 20 de Dic. de 2018
Hi Elliot,
As an alternative to adding coder.extrinsic('rand') to all the blocks, one can also setup a global simulink function in the model. Within the simulink function one can insert a 'MATLAB function' block, which:
  1. Will initialize the seed to a user specified value by reading from a masked parameter
  2. Will draw a random number and return the value
Then in the block's code where one needs a random number, one can invoke this Simulink function instead.
I have attached a model that contains a masked subsystem with a Simulink function, that when called will return a random number.
One can set the seed by calling:
set_param('RandomNumberManager/RandomNumManager', 'Seed', '12345');
- Krishna

Categorías

Más información sobre Discrete-Event Simulation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by