简体   繁体   English

rand()不服从Qt Creator中的srand()

[英]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 我在Qt Creator 1.0.0(Qt版本4.5.0)中编写了一个程序,在main()函数的开头我放了

srand(time(0));

Then I'm calling rand() from another thread (subclass of QThread). 然后我从另一个线程(QThread的子类)调用rand()。 In that function, rand() is producing same sequence of numbers each time I'm running the program. 在该函数中,每次运行程序时,rand()都会生成相同的数字序列。 I'm not running the program multiple times in a second. 我没有在一秒钟内多次运行该程序。

Why is this happening? 为什么会这样?

您需要在每个线程中调用srand,因为种子存储在特定于线程的块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM