Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 635
Donotalo Avatar asked Dec 03 '22 14:12

Donotalo


1 Answers

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

like image 100
Zed Avatar answered Dec 09 '22 15:12

Zed