This is probably a really simple question but I'm pretty new to random number generation on c++ and want to make sure I'm getting it correct.
I have a stochastic function that I want to run in parallel, multiple times, so each parallel run of the function needs to be different from the other and different from previous runs, from my understanding, one way I could do this would be to have random_device as the seed for each. e.g.
for (int i= 0; i< runs; i++){
//do something
#pragma omp parallel for schedule(static)
for (int j = 0; j < std::size(iters); j++){
std::mt19937 mrandThread(std::random_device{}());
iters.at(j) = stochFunction(parameters, mrandThread);
}
//do something
}
However, this seems like it would be computationally expensive, as you would be initiating random_device many times, especially if the above loop is repeated a lot. Another problem would be that runs may be duplicated, as the random_device is just setting a seed, which may come up again? However, currently passing the engine from outside the loops e.g.
std::mt19937 mrandThread(std::random_device{}());
for (int i= 0; i< runs; i++){
//do something
#pragma omp parallel for schedule(static)
for (int j = 0; j < std::size(iters); j++){
iters.at(j) = stochFunction(parameters, mrandThread);
}
//do something
}
means that each thread gives the same result, as they are just running duplicates of the mersenne twister from the point they were sent out in parallel? Another option I've seen is to use rand_r(), but could this again potentially have a problem similar to seed duplication, or does this act more like a branching off of the current random trajectory set outside the loop?
Any advice on how best to implement this would be much appreciated.
Aucun commentaire:
Enregistrer un commentaire