rand不是生成随机数的吗??为什么输出的数不变??
#include
#include
int main(){
int i;
for(i=0;i<10;i++)
printf("%d ",rand());
return 0;
}
函数名: rand
功 能: 随机数发生器
用 法: void rand(void);
程序例:
#include
#include
int main(void)
{
int i;
printf("Ten random numbers from 0 to 99\n\n");
for(i=0; i<10; i++)
printf("%d\n", rand() % 100);
return 0;
}
http://blog.sina.com.cn/s/blog_693301190101355m.html
rand()是伪随机函数,它的结果其实是固定的,只是在大范围内看起来是随机的。它的输出结果根据初始化值的不同而不同。c语言中提供srand( int a)来给rand设置初始值。
如果不设置srand( int a),则系统默认为1,输出结果自然每一次都是一致的。
我的博客有写
http://www.cnblogs.com/lulipro/p/5052621.html