I have a program which use pthreads. In each thread a random number is generated using the rand() (stdlib.h) function. But it seems like every thread is generating the same random number. What is the reason for that??.. Am I doing something wrong??.. Thanks
Share
rand()is pseudo-random and not guaranteed to be thread-safe, regardless, you need to seedrand():See
std::rand()at cppreference.com for more details.A sample program may look like this:
Note how the pseudo-random number generator is seeded with the time only once (and not per thread).