Generation of random numbers in batches

2 次查看(过去 30 天)
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 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