lundi 26 mars 2018

predictable pseudo-random list with multiprocessing

I have a script which uses multiprocessing to create a list of pseudo random numbers. A simplification of which looks like this:

import os, multiprocessing
import numpy as np    

def generate_random_number(i):
    np.random.seed()
    return np.random.uniform(0, 100)

pool = multiprocessing.Pool(os.cpu_count() - 1)
rand_list = pool.map(generate_random_number, range(10))
print(rand_list)

And the output looks like that:

[77.57457867285069, 72.53197844139046, 13.197630136723138, 64.9476430895216, 61.91057931216751, 43.436320344539446, 0.16208332066368625, 11.226294184830632, 20.325312826899765, 28.020558284894005]

In order to do regression testing, I want to be able to seed the randomness, in a fashion which will still give me pseudo random numbers, but the same numbers every time. I tried the following:

def generate_random_number(i):
    return np.random.uniform(0, 100)

    np.random.seed(123)
    pool = multiprocessing.Pool(os.cpu_count() - 1)
    rand_list = pool.map(generate_random_number, range(10))
    print(rand_list)

but then all workers start with the same seed, so I get:

[69.64691855978616, 69.64691855978616, 69.64691855978616, 69.64691855978616, 69.64691855978616, 69.64691855978616, 28.613933495037948, 28.613933495037948, 28.613933495037948, 28.613933495037948]

The same number is always repeated as many times as the pool has workers.

Is there a way for me to seed the randomness in such a way that will always give me the same list, but with a seemingly random list, regardless of the amount of workers?




Aucun commentaire:

Enregistrer un commentaire