I am writing code where random numbers are being sampled from a uniform distribution who's bounds vary for certain iterations within a for loop, e.g.,
std::mt19937 generator{0};
for(int i = 0; i < n; ++i)
{
if(conditions are met)
{
// low and hi bounds change for each iteration
std::uniform_int_distribution<int> U(low, hi);
auto sample = U(generator);
}
}
This is the way I currently have the code written, but it's creating and deleting a temporary std::uniform_int_distribution<int> object for each iteration where the conditions are met. Is this an expensive process? Could compiler optimizations construct the object outside of the for loop, and instead reconstructs the bounds of the uniform distribution within the if statement? I'm not sure if that's necessarily faster.
Are there other approaches that might be better?
Aucun commentaire:
Enregistrer un commentaire