Main Content

Repeat Random Numbers in parfor-Loops

As described in Control Random Number Streams on Workers, each worker in a cluster working on the same job has an independent random number generator stream. By default, therefore, each worker in a pool, and each iteration in a parfor-loop has a unique, independent set of random numbers. Subsequent runs of the parfor-loop generate different numbers.

In a parfor-loop, you cannot control what sequence the iterations execute in, nor can you control which worker runs which iterations. So even if you reset the random number generators, the parfor-loop can generate the same values in a different sequence.

To reproduce the same set of random numbers in a parfor-loop each time the loop runs, you must control random generation by assigning a particular substream for each iteration.

First, create the stream you want to use, using a generator that supports substreams. Creating the stream as a parallel.pool.Constant allows all workers to access the stream.

sc = parallel.pool.Constant(RandStream('Threefry'))

Inside the parfor-loop, you can set the substream index by the loop index. This ensures that each iteration uses its particular set of random numbers, regardless of which worker runs that iteration or what sequence iterations run in.

r = zeros(1,16);
parfor i = 1:16
    stream = sc.Value;        % Extract the stream from the Constant
    stream.Substream = i;
    r(i) = rand(stream);
end
r
r =

  Columns 1 through 8

    0.3640    0.8645    0.0440    0.7564    0.5323    0.8075    0.2145    0.9128

  Columns 9 through 16

    0.4057    0.0581    0.5515    0.4347    0.3531    0.4677    0.8287    0.2312

See Also

|

Related Topics