① c语言随机抽取命令
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void
main()
{
long
a[10000]={0},num;
int
i,count=0,countNum=0;
time_t
start=0,end=0;
srand((unsigned)time(NULL));
for(i=0;
i<10000;i++)
{
a[i]=rand()%100000+1;
printf("%ld
",
a[i]);
count++;
if(count%10==0)
printf("\n");
}
printf("\n请您输入需要查找的数:\n");
scanf("%ld",&num);
printf("\n您需要查找的数在以下位置出现:\n");
start=time(NULL);
for(i=0;
i<10000;i++)
{
if(num==a[i])
{
printf("第%d个\n",i+1);
countNum++;
}
}
end=time(NULL);
printf("共出现了%d次,累计查找时间为%d秒。\n",countNum,start-end);
getch();
}
由于查询速度很快,所以精确到秒级的计时都为0
② 如何用c语言实现抽取随机数
用标准c就可以实现
需要用到的两个函数在包含在stdlib.h头文件里,分别是
void rand ( unsigned int seed ); //设置随机化种子
int rand ( void ); //产生0到RAND_MAX之间的随机数,RAND_MAX是stdlib.h里定义的数字。
其实这两个函数实现产生的是假随机数,所以真正实现随机还要借助time.h里面的函数time()。
具体可以参考下面的方法:
//产生0-9只间的随机数
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
int Random()
{
srand(time(NULL));
return rand()%9;
}
③ c语言怎么生成随机数
你好!
#include<stdio.h>
#include<stdlib.h>//生成随机数用
#include<time.h>//利用时间生成种子
#include<math.h>
intmain()
{
inti;
inta[100];
srand(time(NULL));//生成种子
for(i=0;i<100;i++)
{
a[i]=rand()%1000+1000;//生成一个小于1000的随机数
//然后加1000,变成1000-2000之间的数
printf("%d",a[i]);//打印
}
i=rand()%100;//随机抽取其中的一个数
printf("
抽取到的是:%d
",a[i]);//打印
return0;
}
④ C语言中如何选取随机的数据
srand(time(0));
int
a
=
1
+
(int)rand()/RAND_MAX*99;
在一个数组里把抽取过的数放进去,每次抽时与里面的比较。有相同的重新抽取就可以了。