I have a class like this:
public class Class implements Runnable {
...
public void run() {
ArrayList<Integer> simulations = new ArrayList<>();
ArrayList<Random> randomSeedsList = new ArrayList<>();
for (int run = 0; run < getNumberOfSimulations(); run++) {
simulations.add(run);
Random random = new Random(run);
randomSeedsList.add(run, random);
}
...
simulations.parallelStream().forEach(run -> runSimulation(run,...))
}
}
Now, the runSimulation
does a lot of things, calls other classes, etc. and at many points of execution, this function is called to get the next random number based on the specific run/simulation you are in:
rantInt(arg1,arg2,run)
rantInt is defined in the class below:
public class Util {
// get random integer between min and max (inclusive)
public static int randInt(int min, int max, int run) {
return Class.randomSeedsList.get(run).nextInt((max - min) + 1) + min;
}
}
The thing is that I want every time that I run let say N parallel simulations to have the exact same results for each one and I have got that with this implementation - but it is ugly because I had to pass the run
variable through lots and lots of calls, just so that I will know its value when "someone" from these simulations executes rantInt
.
My question: Is there a more elegant way to do this? Any other design principle that I miss in this implementation that will get me the same results without having to carry the run
variable everywhere?
Aucun commentaire:
Enregistrer un commentaire