How to freeze a random sample

I need to generate a random sample between (0,1) and freeze it so that when I run the program again it generates the same "random" sample. Matlab help gave me:
% Save v5 generator state.
st = rand('state');
% Call rand.
x = rand(1,4);
% Restore v5 generator state.
rand('state',st);
% Call rand again and hope
% for the same results.
y = rand(1,4)
First iteration output is: x =
0.59494 0.27395 0.0481 0.83809
y =
0.59494 0.27395 0.0481 0.83809
But second run produces different sequence: x =
0.10254 0.72827 0.4405 0.99719
y =
0.10254 0.72827 0.4405 0.99719
I need it to give me the same results, so the second run should be: x =
0.59494 0.27395 0.0481 0.83809
y =
0.59494 0.27395 0.0481 0.83809
I know this involves using a seed but the help wasn't clear to me. The rng function doesn't work in my version.

 채택된 답변

Peter Perkins
Peter Perkins 2011년 11월 29일

0 개 추천

Sophia, unless you are using an old version of MATLAB (R2008a or earlier, as I recall) you should not (NOT!) be using any of the rand or randn syntaxes involving the 'seed', 'state', or 'twister' keywords. They continue to work as they have for many years, including allowing you to make the subtle mistake demonstrated in your code and output.
In short, the first line
st = rand('state')
reads the state of a generator that is not currently "active", or at least not unless you have executed a line such as
rand('state',0)
at some earlier point. This is because 'state' does not refer to "the state of the global random number generator", but rather to "the state of the particular generator that used to be MATLAB's default and is still in there if you absolutely insist on asking for it." Even if that mistake is corrected, the code still uses what is an out of date generator algorithm that is no longer recommended. The same is true of 'seed', for an even older and less-recommended generator.
So, as Honglei recommends, if you are using MATLAB R2011a or newer, use the RNG function. All of the reference pages for rand/randn/randi have an example something like this one from rand:
Example 4: Save the settings for the random number generator used by
rand, RANDI, and RANDN, generate 5 values from rand, restore the
settings, and repeat those values.
s = rng
u1 = rand(1,5)
rng(s);
u2 = rand(1,5) % contains exactly the same values as u1
and that's what you want to do. RNG is also described at length here.

추가 답변 (3개)

Walter Roberson
Walter Roberson 2011년 11월 29일

0 개 추천

As an experiment, after your line
st = rand('state');
Try adding
rand('state',st);
That would "restore" the state you just saved. Although that should be the same as what you already do, there is a possibility that some of the complete state information does not get saved, in which case forcing the state to be the same both times should make a difference.
No promises on this, but it is worth a try.
Honglei Chen
Honglei Chen 2011년 11월 29일

0 개 추천

There are some side effects using the syntax Walter mentioned. Basically MATLAB changes the random number generator silently behind the scene.
If you are using a version after R2011a, you should take a look at
doc rng
If you are using a version after R2008b but before R2011a, then you can take a look at
doc Randstream
The two functions the same, but the interface of rng is simpler.
HTH
Fangjun Jiang
Fangjun Jiang 2011년 11월 29일

0 개 추천

What about 'seed'? I always use 'seed' as a way to uniquely identify a series of random data. As long as you specify the same seed, you can get the same series of random data.
>> rand('seed',1);rand(1,5)
ans =
0.5129 0.4605 0.3504 0.0950 0.4337
>> rand('seed',1);rand(1,5)
ans =
0.5129 0.4605 0.3504 0.0950 0.4337
>> rand('seed',1);rand(1,5)
ans =
0.5129 0.4605 0.3504 0.0950 0.4337
>> rand('seed',1);rand(1,5)
ans =
0.5129 0.4605 0.3504 0.0950 0.4337
>> rand('seed',1);rand(1,5)
ans =
0.5129 0.4605 0.3504 0.0950 0.4337

댓글 수: 1

Honglei Chen
Honglei Chen 2011년 11월 29일
It has the same effect as 'state'. When y ou call rand('seed',1), it actually switch to a specific random number generator.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2011년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by