views:

352

answers:

1

I've written a program in Qt Creator 1.0.0 (Qt version 4.5.0) where at the beginning of main() function I've put

srand(time(0));

Then I'm calling rand() from another thread (subclass of QThread). In that function, rand() is producing same sequence of numbers each time I'm running the program. Why is this happening?

Thanks in advanced.

Edit:

I'm not running the program multiple times in a second.

+14  A: 

You need to call srand in each thread, because the seed is stored in a thread-specific block.

Zed
This certainly may be the case. The standard does not guarantee it however, and I somewhat doubt that an implementation that didn't use a common seed would be standard conforming. Always ready to be corrected, however!
anon