c语言随机数函数是 rand()
若要输出1~10之间的随机数,
可用rand()%10+1。
srand -- 设随机种子函数
rand -- 取随机数函数。最大值 是 RAND_MAX。rand 产生的是均匀分布的伪随机数。
例如,产生10个 1-30 间 的 随机 浮点数:
#include
#include
#include
#include
void main()
{
long int i,y;
double x, r, M = 30.0;
srand((unsigned)time(NULL));
for (i=0;i<10;i++){
r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
x = (r * M);
//y = (long int) x;
printf("%lf\n",x);
//printf("%d\n",y);
};
}
random() ,随机生成一个整数。
rand() 返回一个随机整数。