Suppose I have the following code in MATLAB:
clc; clear;
myseed = rng(1);
c = 1; d = 2;
parfor i = 1:1000
a = randn(200,1);
b = randn(50,1);
c*(sum(a)+sum(b));
end
parfor i = 1:1000
a = randn(200,1);
b = randn(50,1);
d*(sum(a)+sum(b));
end
The third line in each loop above captures the processing that I apply to a
and b
, which is specific to the loop and which is very lengthy in my real application. The reason for repeating the randomizing of a
and b
is because I would like to put these 2 loops into 2 separate files that I can call from the main program. I understand there is some inefficiency here but the code would be easier to follow for me.
How do I use rng
so a
and b
in one loop are the same as the a
and b
in another loop? (That is, the 1000 "random" values for a
from the first loop are the same as the 1000 "random" values for a
from the other loop. They don't have to be in the same order.) I tried adding rng(myseed)
between the 2 loops but this didn't help.