# Random numbers in parallel calculations

Assume I have a function that requires random number (noise) like:

@parallel
def foo(i):
print np.random.random()


4 sequential runs yield desired output:

for i in range(4):
foo(i)
0.961718217227
0.909042125122
0.736138778296
0.149902522071


But the parallel run calculates only one random value:

list(foo(range(4)))
0.633760965726
0.633760965726
0.633760965726
0.633760965726
[(((0),{}),None),(((2),{}),None),(((1),{}),None),(((3),{}),None)]


How do I properly generate random values from within parallel function?

edit retag close merge delete

Sort by » oldest newest most voted

This is because numpy uses the same random seed that was computed before (and given in each subprocess). You can reset the seed in each process, so that they will behave independently:

sage: @parallel
....: def foo(i):
....:     np.random.seed()
....:     print np.random.random()
sage: list(foo(range(4)))
0.0290924047484
0.491286471752
0.0812252231074
0.055948998056
[(((0,), {}), None),
(((1,), {}), None),
(((2,), {}), None),
(((3,), {}), None)]

more