Avoiding Repeated seed generation using srand()

被刻印的时光 ゝ 提交于 2019-12-02 09:49:59

Set the seed once, before the loop.

Keep srand out:

srand(time(NULL)); //Initialize seed
for(int i=0; i<npasses; i++)
{
   for(int j=0; j<100; j++)
      printf("%d ", rand()%10);

   printf("\n"); //New line after 100 numbers
}

try this

srand(clock()); //Initialize seed

You can use the random generator to generate a new seed.

For example:

srand((unsigned int)rand());

And use srand(time(NULL)) only once before the loop. But as suggested in another answer, you might as well drop the whole srand inside the loop as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!