필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
Corey Golladay
Corey Golladay 2017년 8월 4일
답변: Steven Lord 2017년 8월 4일
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.

채택된 답변

Teja Muppirala
Teja Muppirala 2017년 8월 4일
Try putting the seed value to:
randi(intmax)
  댓글 수: 1
Corey Golladay
Corey Golladay 2017년 8월 4일
편집: Corey Golladay 2017년 8월 4일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Steven Lord
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.
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.

Anthony
Anthony 2017년 8월 4일
rng('shuffle') ?
  댓글 수: 1
Corey Golladay
Corey Golladay 2017년 8월 4일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by