我有奇怪的行为,其中我尝试生成随机数 ,他们有时完全相同, 或有问题,如果他们在同一时间创建? 使用VC 2012C随机数发生器有时产生相同的数字
for (int i = array_size; i < (array_size); i++)
{
srand((unsigned int)time(NULL));
int rw = rand() % 960 + (20); //returns a pseudo-random integer between x resolution size 0 -> width
int rh = rand() % 640 + (20); //returns a pseudo-random integer between x resolution size 0 -> height
lwsl_notice("c:%d width %d height %d\n",c, rw, rh);
c++;
}
的输出是:
[2016/06/30 09:39:09:7274] NOTICE: c:0 width 606 height 567
[2016/06/30 09:39:09:7274] NOTICE: c:1 width 606 height 567
[2016/06/30 09:39:09:7274] NOTICE: c:2 width 606 height 567
[2016/06/30 09:39:09:7274] NOTICE: c:3 width 606 height 567
你应该在循环之外使用'srand'并且一次。 – ameyCU
oops更正确的dup是[相同的随机数每个循环迭代](http://stackoverflow.com/q/9251117/995714),[rand()在多个函数调用中是一致的](http://stackoverflow.com/q/29478555/995714),[srand(time(NULL))不会快速更改种子值](http://stackoverflow.com/q/5574914/995714),[srand() - 为什么只调用它一次?](http://stackoverflow.com/q/7343833/995714) –