Main Content

Repeat Random Numbers in parfor-Loops

As described inControl 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 aparfor-loop has a unique, independent set of random numbers. Subsequent runs of theparfor-loop generate different numbers.

In aparfor-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, theparfor-loop can generate the same values in a different sequence.

To reproduce the same set of random numbers in aparfor-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 aparallel.pool.Constant允许所有员工访问stream.

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

Inside theparfor-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 = 0 (16);parfori = 1:16 stream = sc.Value;% Extract the stream from the Constantstream.Substream = i; r(i) = rand(stream);endr
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