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

조회 수: 8 (최근 30일)
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.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by