function randomInit( idx )
%randomInit - initialize the random state for a task
%
%   Set each worker to use an even numbered stream based on its index. We
%   use the Threefry random number generator since the streams it
%   provides are statistically independent and are evenly distributed in
%   the state-space.

%  Copyright 2006-2022 The MathWorks, Inc.

[cpuStream, gpuStream] = matlab.internal.parallel.createWorkerRandStream(idx);

% Set the default RNG for the CPU and force the global RNG back to the default.
matlab.internal.math.setDefaultRandStream(cpuStream);
RandStream.restoreDefaultGlobalStream();
 
% Set the GPU random state just in case we need it (note that this doesn't
% cause the GPU libraries to load, so is cheap).
if parallel.internal.general.isGPUSupportInstalled()
    parallel.internal.gpu.randGetSetDefaultStream(gpuStream);
end