Donotalo
Donotalo

Reputation: 13025

rand() doesn't obey srand() in Qt Creator

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. I'm not running the program multiple times in a second.

Why is this happening?

Upvotes: 4

Views: 1968

Answers (1)

Zed
Zed

Reputation: 57658

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

Upvotes: 17

Related Questions