mercredi 31 août 2016

One Random instance per thread, seeded by the original Random instance to attain reproducibility

I need to do 1000 calculations, each one based on a random number. The entire run needs to be reproducible.

In a single threaded environment, I just create random based on a seed and use that for each calculation:

Random random = new Random(37);
for (Calculation c : calculations) {
    c.doCalculation(r.nextInt());
}

In a multi threaded enviromnent, where I have 10 threads, I 'd have a seeded random seed the thread Random's:

Random initRandom = new Random(37);
for (List<Calculation> p : calculationPartitions) {
    final Random threadRandom = new Random(initRandom.nextLong());
    executorService.submit(() -> {
        for (Calculation c : p) {
            c.doCalculation(threadRandom.nextInt());
        }
    });
}

Is this a good idea? Is it still an evenly distributed random in general?

For reproducible reasons, I cannot share 1 global random, as some threads might run faster than others in some runs.




Aucun commentaire:

Enregistrer un commentaire