mercredi 27 mai 2015

srand() + rand() with local scope

I have a function that calls srand and rand like this:

void foo() {
   int seed = some_operation();
   std::srand(seed);
   int value = std::rand();
   // Do something with random value
}

However, I don't want to change the global state of rand. Whats the easiest way to get a random number then?

Requirements:

  • random number must be deterministic based on seed
  • C++11 is fine
  • foo should be thread safe
  • the global state of rand should not be modified

Aucun commentaire:

Enregistrer un commentaire