【发布时间】:2016-07-30 06:44:42
【问题描述】:
我有奇怪的行为,我尝试生成随机数 他们有时完全一样, 或者如果他们同时创建有问题? 使用 VC 2012
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一次。