C语言中 random使用

来源:百度知道 编辑:UC知道 时间:2024/05/22 04:40:20
在C里,random怎样使用?我要随机给一维数组赋值:
for(i=0;i<100;i++)
count[i]=random();
这样好像不对,高手指点

for(i=0;i<100;i++)
count[i]=random();
在random()中写上你想要的随机值——如:random(100),即是0到100中的数
还不懂了,就在baidu中搜一下random()的用法
呵呵~~加油

有些编译器并没有randomize()和random(int)这两个函数。
像我经常使用的devcpp就没有,而turbo c2.0貌似是可以使用的。

/* Note:Your choice is C IDE */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main()
{
int i,count[100];
for(i=0;i<100;i++)
{count[i]=random(1000);//设定取值范围,这表明随机数是0-1000之间取
printf("%d\n",count[i]);
}

}
这样应该满足你的要求了

srand(int)用来设种子,然后每次rand()返回一个随机值

种子最好是每次都不同的,否则你每次得到的都是同样的一系列伪随机数,通常让种子和当前时间相关,比如
srand((unsigned)time(0));

  Random函数
  Math.random();
  可以产生出0-1之间的任意小数,例如0.0105901374530933 或0.872525005541986,有几个其他的函数可以用来改变产生的数字,从而可以更好的在影片中使用:
  Math.round();
  Math.ceil();
  Math.floor();
  这几个函数都是用来取得整数的,Math.round();是采用四舍五入方式取得最接近的整数。Math.ceil();是向上取得