numpy 1.17 just introduced [quoting] "..three strategies implemented that can be used to produce repeatable pseudo-random numbers across multiple processes (local or distributed).."
the 1st strategy is using a SeedSequence object. There are many parent / child options there, but for our case, if you want the same generated random numbers, but different at each run:
(python3, printing 3 random numbers from 4 processes)
from numpy.random import SeedSequence, default_rng
from multiprocessing import Pool
def rng_mp(rng):
return [ rng.random() for i in range(3) ]
seed_sequence = SeedSequence()
n_proc = 4
pool = Pool(processes=n_proc)
pool.map(rng_mp, [ default_rng(seed_sequence) for i in range(n_proc) ])
# 2 different runs
[[0.2825724770857644, 0.6465318335272593, 0.4620869345284885],
[0.2825724770857644, 0.6465318335272593, 0.4620869345284885],
[0.2825724770857644, 0.6465318335272593, 0.4620869345284885],
[0.2825724770857644, 0.6465318335272593, 0.4620869345284885]]
[[0.04503760429109904, 0.2137916986051025, 0.8947678672387492],
[0.04503760429109904, 0.2137916986051025, 0.8947678672387492],
[0.04503760429109904, 0.2137916986051025, 0.8947678672387492],
[0.04503760429109904, 0.2137916986051025, 0.8947678672387492]]
If you want the same result for reproducing purposes, you can simply reseed numpy with the same seed (17):
import numpy as np
from multiprocessing import Pool
def rng_mp(seed):
np.random.seed(seed)
return [ np.random.rand() for i in range(3) ]
n_proc = 4
pool = Pool(processes=n_proc)
pool.map(rng_mp, [17] * n_proc)
# same results each run:
[[0.2946650026871097, 0.5305867556052941, 0.19152078694749486],
[0.2946650026871097, 0.5305867556052941, 0.19152078694749486],
[0.2946650026871097, 0.5305867556052941, 0.19152078694749486],
[0.2946650026871097, 0.5305867556052941, 0.19152078694749486]]