How to generate non-repeatable random number in simulink in same session

I'm trying to use poissrnd to get a random distribution of numbers for determing water usage from a washer using a user defined function block in simulink. With lambda = 0.052, I get the same 'random' numbers everytime time I run the simulation. I tried using rng('shuffle') in the function block, which works but causes run-time to shoot dramatically up. I also tried simply typing rng('shuffle') into the initfunction and startfunction for the model setting, but that didn't work at all, ie same results over again.
Did some reading around of questions posted about this topic, and the closes I could find is here. How to achieve non-repeatable randomization in Stateflow? But I'm lost as to how to create the mat file, and where to save it, and how to work with the init function to call (?) the mat file to generate a random seed for each simulation run in the same session.
Can anyone provide any guidance? Thank you.

 Accepted Answer

The MATLAB Function block is executed at every simulation step. For this case, I would suggest using rng() and poissrnd(0.52,N,1) to generate data offline and then import it into Simulink using "From Workspace" block.

5 Comments

Thanks for the response. When you say use rng(), do you mean rng('shuffle')? When I type in rng(), all that shows up is the characteristics of the current seed.
And can you explain why the N is necessary? I am planning on changing runtimes, so N will be changing as well. The function block adapts well to these changes, since it runs only depending on sampling times. Whereas creating a single vector beforehand requires that I know the appropriate vector length, correct?
Also, for some reason, the data from the workspace is not being loaded by the workspace block in simulink.
Even though, the workspace block looks like it can identify the vector from the workspace to begin with:
Any protips?
If you are NEVER going to need to repeat the same simulation, you can use rng('shuffle'). If you might need to repeat the simulation, use rng(n) where n is an integer number and later you can re-call this n to repeat the simulation.
Set tStop, tStep etc. as parameters and then set up your model's stop time and step size as tStop, tStep. Create the below variables in base workspace
time=[0:tStep:tStop].';
rng(1);
data=poissrnd(0.52,size(time))
and then use a "From Workspace" block and specify the data as [time, data]
Okay, great thank you for the response, but I wasn't able to figure it out I think. Just in case I did something wrong or in case other people after me see this response, this is what I did to set the stop time and step size parameters in the simulations. I went to model settings and under the 'Solver' category, I changed the Stop time variable to tStop and the changed the type of solver selection to Fixed-step and inserted tStep as the variable for the Fixed-step size (fundamental sample time) variable.
I created a .m file with the preset initial condition I ran for this simulation.
However, when I ran the simulation, everything seemed to go okay, except when I tried to open a scope attached to the [time, data] block. I was told that it had '1001 signals' and 'may take several minutes or hours to visualize'. I wasn't able to get a picture of that, because simulink froze.
Any guidance?
Edit: I was able to open up part of the scope view, but it didn't tell me much.
Edit 2: Scope loaded, can see why the previous warning popped up:
"time" needs to be a column vector. time=[0:tStep:tStop].';
Great that did the trick! Btw, I ended up not using poissonrnd and rather PoissonSamp, as that got me consistently random numbers.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!