MATLAB: How to achieve non-repeatable randomization in Stateflow

randirandom number generatorrandom sourcerng('shuffle')simulinkstateflow

In R2012b I created a Stateflow chart (Matlab syntax) containing several random function calls ( rand(1,1), randn(1,1) and randi(n,1)) to model uncertainties. This works well, but each time I run a simulation, it returns exactly the same results because by default the random generator resets itself before each simulation. This is not what I want: I want to have different results every time. The Matlab function rng('shuffle') seems to be exactly what I need but it isn't compatible with Stateflow or Simulink. Typing rng('shuffle') in the Matlab command window before each simulation run doesn't work either. The only option I seem to have is to use input from Simulink Random Source blocks because they have a 'repeatability' setting. Random sources may relatively easily replace rand(1,1) or randn(1,1), but not randi(n,1). Is there a more elegant solution – preferably a way to prevent resetting of the random generator?

Best Answer

Thanks for your suggestions. Actually I want non-repeatability for simulation runs within one session, OTOH non-repeatability for each session might come in handy if Matlab crashes in between... I wanted to avoid Simulink input for random numbers altogether but if that's not possible, the Random Source block turns out to do the job quite well if I set its 'repeatability' to 'non-repeatable'. I don't know what'll happen between sessions - so far Matlab hasn't crashed between simulation runs - I have to keep my fingers crossed.