generation of fixed random variables
7 ビュー (過去 30 日間)
古いコメントを表示
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,
回答 (2 件)
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
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
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!