Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 24 Juin 2020
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 commentaires
Fangjun Jiang
Fangjun Jiang le 24 Juin 2020
"time" needs to be a column vector. time=[0:tStep:tStop].';
Ian Van Giesen
Ian Van Giesen le 25 Juin 2020
Modifié(e) : Ian Van Giesen le 25 Juin 2020
Great that did the trick! Btw, I ended up not using poissonrnd and rather PoissonSamp, as that got me consistently random numbers.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by