MATLAB Answers

0

Why is getting Uniform Random Numbers so difficult!?!?

Asked by Corey Golladay on 4 Aug 2017
Latest activity Answered by 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

Sign in to comment.

3 Answers

Answer by Teja Muppirala
on 4 Aug 2017
 Accepted Answer

Try putting the seed value to:
randi(intmax)

  1 Comment

Wow, it worked! Thank you very much. Who would have thought that to make a random number generator work, you'd have to input a command to create a randomly generated seed.

Sign in to comment.


Answer by 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:
  1. Open a new blank model.
  2. Connect a Uniform Random Number block to a Scope block.
  3. Set the Seed of the Uniform Random Number block to x.
  4. Define a variable x in the base workspace using x = 0;
  5. 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

Sign in to comment.


Answer by Anthony on 4 Aug 2017

rng('shuffle') ?

  1 Comment

I tried it and can't get it to control the urng "Seed" parameter either. Thanks though. I also tried using a variable name r_seed in the "Seed" field and it won't pull a randomly generated variable from the workspace either.

Sign in to comment.