:
对自己的随机数的设置
1、可以利用头文件对随机数设置种子,随后利用rand函数对其进行随机设置
例如:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a , seed;
printf(“Pelase input seed:”);
scanf("%d",&seed");//设置随机数种子
srand(seed);
a = rand()%100+1\设置1到100的随机数
printf("%d\n",a);
return 0;
}
2、可利用time头文件为rand函数随机种子
#include<stdio.h>
#include<stadlib.h>
#include<time.h>
int main()
{
int a;
srand(time(NULL);
a = rand() %100+ 1;
printf("%d",a);
rerurn 0;
}
当然这种方法就不用了输入随机数种子。
2、后面我们可以对该随机数做一个简单地运用、
假如我们可以为小朋友做一个乘法的简单运算
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
main()
{
int a,b,sum,t,count = 0;
srand(time(NULL));
do
{
count+=1;
a=rand() % 10 + 1;
b=rand() % 10 + 1;
printf("%d * %d = “,a,b);
sum = a * b;
scanf(”%d",&t);
if(sum != t)
{
printf(“Your Are wrong”);
}
else
printf(“Right”); break;
}while(sum ==t||count<3); \错上三次就会跳出循环
if(count >3)
printf(“Wrong,you are tried threetimes ,Over”);
}
ok,以上就是我的分享,如果有错误及需要改进的地方,请不吝赐教。