I have several threads which all run the same function. In each of these they generate a different random number several times. We tried to do this by putting srand(time(0))
at the start of the function, but it seems that they all get the same number.
Do we need to call srand(time(0))
only once per program, i.e at the start of main
(for example), at the start of each function that is called several times, or something else?
That's a good question. I can't directly answer it because I think there are bigger issues. It doesn't even seem to be clear that rand is thread safe at all anyway. It maintains internals state and it doesn't seem to be well defined if that's per process or per thread, and if it's per process if it's thread safe.
To be sure I would lock a mutex around each access.
Or preferably use a better defined generate such as one from boost
They all get the same number because you presumably start all the threads at the same time, or they are all using the same static seed, in which case you are a bit stuffed. You need a better source of entropy than time(). However, a quick hack would be to seed with (time * thread-id) where thread-id is the id of each worker thread.
Fo course, the correct solution in C++ is is not to use random number generator functions, but to use random number generator objects, like those provided by the Boost random number library, which by their very nature (because they are stack based) are thread-safe. See this answer I prepared earlier for an example. However, there may still be a problem providing sufficient entropy in an MT program, as using time() will still have the problem I mentioned above.
From the
rand
man page:So don't use it with threaded code. Use
rand_r
(ordrand48_r
if you're on linux/glibc). Seed each RNG with a different value (you could seed a first RNG in the main thread to produce random seeds for the ones in each thread).srand() seeds the random number generator. You should only have to call
srand(time(NULL))
once during startup.That said, the documentation states:
The emphasized part of the above is probably the reason why all your threads get the same number.
If you are launching the threads all at the same time, the time sent to srand is probably the same for each thread. Since they all have the same seed, they all return the same sequence. Try using something else like a memory address from a local variable.
As you are using C++, rather than C, you may be able to avoid the threading problems often associated with srand/rand by using c++11. This depends on using a recent compiler which supports these features. You would use a separate engine and distribution on each thread. The example acts like a dice.
I referred to Wikipedia C++11 and Boost random when answering this question.