Generation of random numbers in batches

1 回表示 (過去 30 日間)
NaN
NaN 2020 年 7 月 20 日
編集済み: Adam Danz 2020 年 7 月 20 日
Hello,
Imagine the following code in Matlab
rng(1)
a = rand(1,100)
rng(1)
a2 = rand(1,200);
These are two random generators initialized with the same seed. The elements in a are equal to the 100 first elements in a2.
Now, I want to create a random sequence a3 that satisfies a2 = [a, a3].
How should I choose the seed of a3 to satisfy a2? is this even possible?
Best

採用された回答

NaN
NaN 2020 年 7 月 20 日
Found the answer.
Just need to store the state of the generator with s = rng and initialize with rng(s) the next generation
Thanks for your help

その他の回答 (3 件)

Steven Lord
Steven Lord 2020 年 7 月 20 日
The way to do what you're asking is to record the state of the random number generator. You can't really "work backwards".
>> rng(1)
>> a = rand(1, 100);
>> s = rng;
>> rng(1)
>> a2 = rand(1, 200);
>> rng(s) % Use the state recorded after generating the first 100
>> a3 = rand(1, 100);
>> isequal([a a3], a2)
ans =
logical
1

Adam Danz
Adam Danz 2020 年 7 月 20 日
編集済み: Adam Danz 2020 年 7 月 20 日
The seeds below are chosen arbitrarily.
rng(1)
a = rand(1,100);
rng(2)
a3 = rand(1,100);
a2 = [a,a3]
You don't even have to change seeds if the a and a3 are generated consecutively.
rng(100)
a = rand(1,100);
a3 = rand(1,100);
a2 = [a,a3];

Bjorn Gustavsson
Bjorn Gustavsson 2020 年 7 月 20 日
if you know from the start how many point in each set just do something like:
a2 = randn(1,num_points_in_a2);
a = a2(1:min(num_points_in_a,numel(a2)));
a3 = a2((1+num_points_in_a):end);
If you dont know how many point you want beforehand just create and throw away the excess points you need - if it is as few as a couple of 100 elements bothering about avoiding might take more time than will be lost...

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by