Why is getting Uniform Random Numbers so difficult!?!?
3 ビュー (過去 30 日間)
I am trying to run a 5 sec simulation iteratively in Simulink with a Uniform Random Number block, (easy, right?). Unfortunately, the "random" number doesn't change. I can't get the seed to change, or Fast Restart to change the generated number. Every time I run the simulation from Simulink or Matlab, I just keep getting the same number, unless I change the seed manually.
その他の回答 (2 件)
Steven Lord 2017 年 8 月 4 日
There are two different behaviors you might want from a random number generator.
In some cases, you might want to be able to rerun a particular section of code or simulate a model a second time and reproduce the exact same results in order to debug or investigate some interesting or unusual behavior. For that you want to Generate Random Numbers That Are Repeatable. That is the behavior if you specify a constant value as the seed in the block.
In other cases, like the one you described, you want to receive different results each time you run your code or simulate the model. For instance, if you were doing some sort of Monte Carlo simulation you don't want the results of the simulation to be identical each time -- that defeats the purpose of Monte Carlo. For that you want to Generate Random Numbers That Are Different. This is the behavior Teja's answer supports.
To support both use cases, you might want to define that parameter of the block to be a variable whose value you change (and record or display) in the model InitFcn. That way you know the specific state of the random number generator with which the results were generated, to which you can set the block parameter if you need to debug or investigate your results.
- Open a new blank model.
- Connect a Uniform Random Number block to a Scope block.
- Set the Seed of the Uniform Random Number block to x.
- Define a variable x in the base workspace using x = 0;
- Set the model's InitFcn to x = x+1;
Each time you simulate the model you will see a different random signal in the Scope because it starts with a different seed, but you can regenerate a particular random signal by changing x in the base workspace. I'm sure there are more sophisticated techniques you can use involving model workspaces, etc. but this should be straightforward enough to demonstrate the general idea.