用C语言怎样产生10个不同的随机数

用C语言怎样产生10个不同的随机数

srand(time(0));
/*设置种子*/
n=rand();
这样就可以产生0到7FFF之间的任意随即数了。注意加上头文件#include<time.h>就好了
要产生10个的话,一个for循环
int
a[10];
for(i=0;i<10;i++)
a[i]=rand();
不是写的明白了吗,srand你没写啊,要先设置种子,编译器里如果不设置种子,种子会默认为0,那么每次运行的随即数都一样的。用time函数来改变种子,就可以很好的体现出“真正的随即”了。
完整代码:
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
main(){
int
i,a[10];
srand(time(0));
for(i=0;i<10;i++)
a[i]=rand();
for(i=0;i<10;i++)
printf("%d\n",a[i]);
}
NOW
OK?
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答