generation of fixed random variables

5 views (last 30 days)
som
som on 25 Apr 2012
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,

Answers (2)

Jan
Jan on 25 Apr 2012
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 Comment
Peter Perkins
Peter Perkins on 25 Apr 2012
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).

Sign in to comment.


Peter Perkins
Peter Perkins on 25 Apr 2012
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.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!