필터 지우기
필터 지우기

generation of fixed random variables

조회 수: 1 (최근 30일)
som
som 2012년 4월 25일
Hi all, I want to generate 12 sets of random variable whose elements don't change in each run. I tried with following commands but they don't work properly.
num_data_gnrn=1200;
inflow='inflow.txt';
q=load(inflow);
T=size(q,2); % T=12
mue=mean(q);
stdvn=std(q);
randn('state');
for t=1:T
q_genrated_init(:,t)=fix( mue(t) + stdvn(t)*randn num_data_gnrn,1));
end
index_out=q_genrated_init<0;
q_genrated_init(index_out)= -1*q_genrated_init(index_out);
I mean I want to generate 12 different sets of random variables whose elements be fixed in every run. Because these data are inputs of other codes and must be fixed. How can I do this. cheers,
  댓글 수: 1
Jan
Jan 2012년 4월 25일
Which Matlab version are you using?

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

답변 (2개)

Jan
Jan 2012년 4월 25일
randn('state') replies the current state. Due to the trailing semi-colon the output is suppressed. You want to set the state instead to get reproducible random numbers:
randn('state', 12345)
Please note, that this syntax is subject to frequent changes, see: http://www.mathworks.com/help/techdoc/math/bsn94u0-1.html.
Another idea: Instead of loading the file inflow.txt, you could create the random numbers once and save it to a file, which is read afterwards everytime the program is called.
  댓글 수: 1
Peter Perkins
Peter Perkins 2012년 4월 25일
Unless you are using a version of MATLAB prior to R2008b, the syntax randn('state', 12345) is strongly discouraged. It will configure the global random number generator to setting that are not recommended.
If you are using R2008b-R2010b, see the doc for RandStream, and use something like reset(RandStream.getDefaultStream).
If you are using R2011a or later, see the doc for the rng function, and use something like rng(12345).

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


Peter Perkins
Peter Perkins 2012년 4월 25일
If you want the same values for each iteration of the loop, you should generate the "random" values before the loop and assign to one or more variables that are used inside the loop.
z = randn(num_data_gnrn,1);
for t=1:T
q_genrated_init(:,t)=fix( mue(t) + stdvn(t)*z);
end
Perhaps that's not what you meant. If you want the same values every time you run the loop, then see my comment to Jan's post.

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by