How to achieve non-repeatable randomization in Stateflow?
이전 댓글 표시
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?
채택된 답변
추가 답변 (1개)
Azzi Abdelmalek
2013년 1월 23일
편집: Azzi Abdelmalek
2013년 1월 23일
You can use a random source u, then in your chart add
n=12
ceil(u*n)
%it's the same as randi(n)
댓글 수: 6
Walter Roberson
2013년 1월 23일
That does not sound like a way to randomize the seed used for the random number generator ?
Azzi Abdelmalek
2013년 1월 23일
편집: Azzi Abdelmalek
2013년 1월 23일
You are right Walter, It's possible to change the seed every simulation, using Uniform Random Number block

set_param('untitled1/Uniform Random Number','Minimum','0')
set_param('untitled1/Uniform Random Number','Maximum','100')
set_param('untitled1/Uniform Random Number','Seed',num2str(randi(100)))
We can also, add those lines of code to Init Fcn in callbacks (Model properties)
Walter Roberson
2013년 1월 24일
Using the pseudo-random number generator to generate a seed for the pseudo-random number generator effectively turns the process into being deterministic. Wilfred wants the results to vary every time, including from session to session.
Azzi Abdelmalek
2013년 1월 24일
편집: Azzi Abdelmalek
2013년 1월 24일
The third line of the code, I've posted in the above comment
set_param('untitled1/Uniform Random Number','Seed',num2str(randi(100)))
changes the seed every time and randomly. Added to the init fcn of the model's calbacks, every simulation, the seed is uptadated.
Walter Roberson
2013년 1월 24일
Is randi() a true random number generator? I don't think so. It is the pseudo-random number generator, and it is going to be working off of whatever random number seed is in effect right then. Which, for the first run after loading, is going to have been set by the default random number seeding in MATLAB, which uses the same value each time (not based upon time or anything like that.) And if you quit and re-enter and re-run then that session will have the default seed, so randi(100) is going to generate exactly the same number as your previous run. Thus you are going to get exactly the same seeding for each session, which is what the original poster does not want to have happen. The original poster wants different seeds even for the very first run after starting each session of MATLAB.
Azzi Abdelmalek
2013년 1월 24일
I did'nt read "For each session" in the actual post. It's possible to create a mat file:
sid=ranperm(1000)
save sid_file sid
In the init function
load sid_file
sid1=sid(1)
sid(1)=[]
save sid_file sid
set_param('untitled1/Uniform Random Number','Seed',num2str(sid1))
set_param('untitled1/Uniform Random Number','Minimum','0')
set_param('untitled1/Uniform Random Number','Maximum','100')
카테고리
도움말 센터 및 File Exchange에서 Sources에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!