How to randomize simulation seed in simulink?
12 views (last 30 days)
Show older comments
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.
0 Comments
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)
coder.extrinsic('pearsrnd');
y = 0; % specifies size and type of y
kurt = skew^2 + 3;
r = pearsrnd(mu,var,skew,kurt,1,1);
y = r;
end
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
rng(100)
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.
3 Comments
Stefanie Schwarz
on 27 Sep 2022
Edited: Stefanie Schwarz
on 27 Sep 2022
Hi Paul,
As long as a MATLAB function is capable of generating C code, it can be used in a MATLAB Function block without the use of coder.extrinsic. That's the case for pearsrnd (see Extended Capabilities section at the bottom).
Running a simulation in Desktop Real-Time Kernel Mode means that we need to generate C code from the entire model to compile it and then execute it in a real-time kernel installed on the host computer. This workflow unfortunately won't allow extrinsic functions. But your comment will still be very useful for "regular" Simulink users! Thanks for that!
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.
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:
rng('shuffle');
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)
rng('shuffle');
kurt = skew^2+3;
r = pearsrnd(mu,var,skew,kurt,1,1);
y = r;
Please refer to the below documentations for more information.
- Control random number generator - MATLAB rng (mathworks.com)
- Generate Random Numbers That Are Different - MATLAB & Simulink (mathworks.com)
Hope this helps!
1 Comment
See Also
Categories
Find more on Naming Conventions 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!