The typical modern approach to get random numbers is like this:
std::random_device rd;
std::mt19937 engine{rd()};
std::uniform_int_distribution<> dis{-10,10};
someInt = dis(engine);
I figure that the std::random_device
object is only used once to seed the pseudo-random engine. I think therefore we can write this instead:
std::mt19937 engine{std::random_device{}()};
std::uniform_int_distribution<> dis{-10,10};
someInt = dis(engine);
The later one does not keep the std::random_device
object alive, and has the advantage of relieving the pain of coming up a name. But I find that a lot of examples on the Internet use the former version.
Is that a must, or are there reasons, to keep the std::random_device
object alive and give it a name?
Aucun commentaire:
Enregistrer un commentaire