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
Edit:
There is a stackoverflow question asking how to generate random numbers. The accepted answer however shows how to generate truly unique random numbers, using a slow std::random_device. I just needed a simple generator using a fixed seed instead.