How do I use a different random number seed in a for each subsystem?

13 ビュー (過去 30 日間)
James
James 2021 年 11 月 12 日
コメント済み: James 2021 年 11 月 16 日
I'm trying to run a variety of random number calls inside of a for-each subsystem. As a simple example, consider this system:
And inside the for each:
In that random number generator I do:
So, the problem is that the "for each" subsystem uses the same random number seeds for each iteration. There are some work-arounds, but they will require large code changes to the "real" code I'm trying to put in a "for-each" subsystem. For example, the "for-each" system can be masked with a seed parameter vector, which can be partitioned and used in the Seed input to the random number. But this requires each seed to be explicitly defined. I was just curious if anyone else had a better solution.

回答 (1 件)

Salman Ahmed
Salman Ahmed 2021 年 11 月 16 日
Hi James,
From my understanding, you wish to generate a random number with a different seed for each different iteration. I think this could be achieved using a MATLAB function block that replaces your random generator block as shown below.
The logic of the MATLAB function block can be written as:
function y = fcn(seed)
y=0;
coder.extrinsic('randi');
switch seed % Define random number seeds for each iteration
case 1
y = randi(1e6);
case 2
y = randi(2e6);
case 3
y = randi(3e6);
case 4
y = randi(4e6);
case 5
y = randi(5e6);
otherwise
y = randi(6e6);
end
This behaviour is caused because the function blocks are compiled into MEX files rather than being evaluated line by line as in MATLAB. The seed for the random number generator remains the same across each simulation since it is the same in the MEX file.
Note that the line "coder.extrinsic('randi')" be included at the top of your MATLAB Function Block script as shown. This will declare the random number generator function as extrinsic, and therefore generate a call to MATLAB's "randi" function, rather than including the code for this function inside the MEX file.
  1 件のコメント
James
James 2021 年 11 月 16 日
Hi Salman,
Thank you for taking the time to look at this. I think my confusion comes from the way that things don't scale the way you'd expect with a for-each.
Consider the example of setting the "Seed" parameter of the random number generator using a "randi(1e6)" call. My understanding is that in a normal simulation (not a for-each) the randi call is made at model initialization, and uses Matlab's rng stream. If you have multiple random number blocks initialized this way, you get different random streams. If, in-between simulations you reset matlab's rng stream (e.g. by calling rng(1)), those random number blocks will produce identical streams for each simulation run.
But what is happening inside of a "for each" system? It seems like each iteration of the for-each "shares" the same call to Matlab's rng stream. Is this intended behavior? Is there a way to make it not "share"?
Also, I'm hesitent to call your response a solution. Essentially you're performing a different operation based on the seed input. I think that it would produce a distinct "y" regardless of whether the "randi" call was made intrinsically or extrinsically.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by