欢迎光临散文网 会员登陆 & 注册

C语言学习笔记:随机数代码篇(第一话)

2019-10-23 00:01 作者:诗书画唱  | 我要投稿

随机数代码:

#include <stdio.h>//导入包

#include <stdlib.h> 

#include <time.h> 

int main()

{

srand((int)time(0));//用当前时间当做随机数的种子 


int a;

a=rand()%5+5;//范围为5到(5+5-1)   a=rand()%5;//0到5 

 

printf("%d",a);


return 0;

}





#include <stdio.h> 

#include <stdlib.h> 

#include <time.h> 

int main()

{

srand((int)time(0));


int a;

a=rand()%13+1;//1到(13+1-1)

//printf("%d",a); 

//printf("请输入出现的数");

// ;

//scanf("%d",&a);


switch(a){

case 1:printf("电脑出了一张红桃%d ",a);break;

case 2:printf("电脑出了一张红桃%d ",a);break;

case 3:printf("电脑出了一张红桃%d ",a);break;

case 4:printf("电脑出了一张红桃%d ",a);break;

case 5:printf("电脑出了一张红桃%d ",a);break;

case 6:printf("电脑出了一张红桃%d ",a);break;

case 7:printf("电脑出了一张红桃%d ",a);break;

case 8:printf("电脑出了一张红桃%d ",a);break;

case 9:printf("电脑出了一张红桃%d ",a);break;

case 10:printf("电脑出了一张红桃%d ",a);break;



case 11:printf("电脑出了一张红桃J");break;

case 12:printf("电脑出了一张红桃Q");break;

case 13:printf("电脑出了一张红桃K");break;



default:printf("输入错误");

}



 


C语言学习笔记:随机数代码篇(第一话)的评论 (共 条)

分享到微博请遵守国家法律