How to randomize simulation seed in simulink?

5 views (last 30 days)
I have a simulink simulation which contains a custom matlab function to generate random numbers based on the pearsrnd.
function y = skewedPRNG(mu, var, skew)
kurt = skew^2+3;
r = pearsrnd(mu,var,skew,kurt,1,1);
y = r;
However, since the simulink seed is fixed, I'm getting the same random signal. How can I change the simulation's seed randomly so that I will get a different signal every time I run the simulation?
Note, I need to generate the random numbers according to a skewed distribution, so using the available random number blocks is not helpful.
Edit: the simulation is Desktop Real-Time. I neglected to mention, sorry.
Edit 2: Desktop Real-Time Kernel Mode, sorry again.

Answers (2)

Paul on 27 Sep 2022
Edited: Paul on 27 Sep 2022
Hi Amr,
When I first saw the question, I was quite surprised. To my uderstanding, the call to pearsrnd in the Matlab Function block should be executed in Matlab, and therefore would need to be declared extrinsic. It isn't, but the simulation still compiles and runs to yield, as you noted, the same output every time. I can't explain this result and, frankly, I'm surprised that there isn't an error when the simulation is being built. I'm very curious as to what is happening. I'm even more curious that rng('shuffle') works, even if the simualation runs slowly.
If you ever do get an explanation as to how all of that works, please post back.
Having said that, if you do declare pearsrnd as extrinsic, like this
function y = skewedPRNG(mu, var, skew)
y = 0; % specifies size and type of y
kurt = skew^2 + 3;
r = pearsrnd(mu,var,skew,kurt,1,1);
y = r;
then the random numbers generated from pearsrnd are derived from the base Matlab random number generator and the results will change from one run to the next. If you want repeatability, you can control the seed in the usual way, typically with a call to rng, before the simulation executes
If you go down this path, you'd have to set the base Matlab seed prior to each simulation execution, either manually if using the play button or the sim command from the command line, or as a command in the script or function if using the sim command in one of those, or in a callback function.
Of course, this seed will affect everything in the simulation that uses the base Matlab random numbers.
Also, after the simulation runs the Matlab random number generator will be in a different state than when the simulation started. If that's a problem you may want to consider saving the random number generator state before the simulation executes and then resetting it after the simulation ends.
As far as I know, Stats toolbox functions always use the default random number stream, so we can't create a new randstream object that would be used just for the simulation, and thereby not "corrupt" the default stream.
I'm afraid I don't know anything about "Desktop Real-Time" and so don't know how that part of the Question might impact the solution.
Paul on 27 Sep 2022
Hi Stefanie,
Thanks for pointing out that Extended Capabilities section.
So for the original case, where coder.extrinsic is NOT used, what is happening? What random stream is pearsrnd using in that case and how does the user control the seeding of that stream?
I'm asking about "regular" Simulink use.

Sign in to comment.

Bhavana Ravirala
Bhavana Ravirala on 26 Sep 2022
Edited: Stefanie Schwarz on 27 Sep 2022
To generate different random signal for every simulation, you can execute the following command:
This results in a different sequence of random numbers after each call to rng. So, you can find the following function useful for your workflow.
function y = skewedPRNG(mu, var, skew)
kurt = skew^2+3;
r = pearsrnd(mu,var,skew,kurt,1,1);
y = r;
Please refer to the below documentations for more information.
Hope this helps!
  1 Comment
Amr Okasha
Amr Okasha on 26 Sep 2022
Edited: Amr Okasha on 26 Sep 2022
slows down the simulation immensly unfortunately.
I'm hoping to use this for a desktop real-time application. It does not build in a desktop real-time environment.
I'm looking for something that will randomize the rng at the start of every run for the entire simulation, not just a block or function.

Sign in to comment.


Find more on Simulink Coder in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by