Why is getting Uniform Random Numbers so difficult!?!?
12 views (last 30 days)
Show older comments
Corey Golladay
on 4 Aug 2017
Answered: Steven Lord
on 4 Aug 2017
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.
0 Comments
Accepted Answer
More Answers (2)
Steven Lord
on 4 Aug 2017
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.
For example:
- 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.
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!