Generation of random numbers in batches

2 views (last 30 days)
NaN
NaN on 20 Jul 2020
Edited: Adam Danz on 20 Jul 2020
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

Accepted Answer

NaN
NaN on 20 Jul 2020
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

More Answers (3)

Steven Lord
Steven Lord on 20 Jul 2020
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 on 20 Jul 2020
Edited: Adam Danz on 20 Jul 2020
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 on 20 Jul 2020
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...

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!