MATLAB: Am I receiving the same results when I run a simulation on a Linux cluster that uses the RAND command in MATLAB 7.13 (R2011b)

MATLAB

I am running a simulation on a Linux cluster. In the simulation I use the RAND command but frequently the simulation yields the same results. I have configured the RAND command in the following way:
reset(RandStream.getDefaultStream, sum(100*clock))
however, I still frequently receive the same results.

Best Answer

The reason why the results are frequently the same is because the different jobs at the different nodes of the Linux cluster are starting at the same time. Therefore, the seed given by the clock to the random number generator algorithm is the same.
A possible way to work around this issue is to select the seed of the random number generator in such a way that not only it depends on the clock, but it depends on another variable which is different for every MATLAB session running in parallel.
If you are using Parallel Computing Toolbox (PCT) a simple solution can be the following:
s = RandStream('name_of_algorithm', 'seed', sum(100*(1+labindex/numlabs)*clock));
RandStream.setGlobalStream(s)
where NUMLABS returns the total number of labs currently operating on the current job and LABINDEX returns the index of the lab currently executing the function.
If you are not using PCT but you are starting different MATLAB sessions from a script, a simple solution can be the following:
x = feature('getpid');
s = RandStream('name_of_algorithm', 'seed', sum(x*clock));
RandStream.setGlobalStream(s)
where the command FEATURE('GETPID') returns a different identification number for each MATLAB session running.