Generation of random numbers in batches
Show older comments
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
More Answers (3)
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
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
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!