I'm trying to generate random numbers within a multiprosses function. My issue is I need to seed the first part of the random generation but not the second part. What I tried is unseed the generator by picking a random Int (np.random.seed(np.random.randint(100000000))
), but because I first seeded the generator, I pick the same Int for the rest of the generation.
So I get the same sequence for each process.
0 [0.5488135 0.71518937 0.60276338 0.54488318 0.4236548 ]
0 [0.91989807 0.99511873 0.6750629 0.60976887 0.65852849]
1 [0.5488135 0.71518937 0.60276338 0.54488318 0.4236548 ]
1 [0.91989807 0.99511873 0.6750629 0.60976887 0.65852849]
2 [0.5488135 0.71518937 0.60276338 0.54488318 0.4236548 ]
2 [0.91989807 0.99511873 0.6750629 0.60976887 0.65852849]
Here a MWE
import numpy as np
import multiprocessing
import random
class mp_worker_class():
def __init__(self,):
pass
@classmethod
def start(self, nb=None, seed=None, nbcore=None):
lfp_p=np.empty((nbcore,nb))
pipe_list = []
for h in range(nbcore):
recv_end, send_end = multiprocessing.Pipe( )
p = multiprocessing.Process(target=self.mp_worker , args=(h, nb, seed, send_end ))
p.start()
pipe_list.append(recv_end)
for idx, recv_end in enumerate(pipe_list):
lfp_p[idx,:]=recv_end.recv()
return lfp_p
@classmethod
def mp_worker(self,h, nb=None, seed=None, send_end=None):
np.random.seed(seed)
np.random.seed(0)
print(h,np.random.rand(5))
#trying to undo the seed
np.random.seed(np.random.randint(100000000))
print(h, np.random.rand(5))
send_end.send(np.random.rand(5))
return
if __name__ == '__main__':
print(mp_worker_class().start(nb=10, seed=1, nbcore=3 ))
Aucun commentaire:
Enregistrer un commentaire