<pre class=\"replyask-text\" id=\"content-2031534\">rand()函数是不能确保随机的结果序列不同(这点可以从printf(\"%d\",rand());
看打印结果,每次都一样,还需要所谓的种子(seed)产生器函数void srand(unsigned int seed);
使用参数做种子来使rand()函数的结果序列不同.参数最好每次都能变化,
那么运行rand()函数的使用,由于srand函数的种子参数不同,
rand()函数随机的结果就不同了.
例子:
#include stdio.h
#include stdlib.h
#include time.h
int main(void)
{
srand(time(NULL));
int num = rand()0;
printf(\"num = %d\\n\", num);
return 0;
}
该例子生成从0到100的随机数。 |