当前位置: 首页 > news >正文

[C语言]——分支和循环(4)

目录

 一.随机数生成

1.rand 

2.srand

3.time

4.设置随机数的范围

猜数字游戏实现


写⼀个猜数字游戏
游戏要求:
(1)电脑自动生成1~100的随机数
(2)玩家猜数字,猜数字的过程中,根据猜测数据的⼤⼩给出⼤了或⼩了的反馈,直到猜对,游戏结束

 一.随机数生成

要想完成猜数字游戏,⾸先得产⽣随机数,那怎么产生随机数呢?

1.rand 

C语言提供了⼀个函数叫 rand,这函数是可以生成随机数的,函数原型如下所示: 

int rand (void);

rand函数会返回⼀个伪随机数,这个随机数的范围是在0~RAND_MAX之间,这个RAND_MAX的⼤⼩是依赖编译器上实现的,但是⼤部分编译器上是32767。

 rand函数的使用需要包含⼀个头文件是:stdlib.h

 那我们就测试⼀下rand函数,这⾥多调⽤⼏次,产⽣5个随机数:

#include <stdio.h>
#include <stdlib.h>
int main()
{
  printf("%d\n", rand());
  printf("%d\n", rand());
  printf("%d\n", rand());
  printf("%d\n", rand());
  printf("%d\n", rand());
  return 0;
}

我们可以看到虽然⼀次运⾏中产⽣的5个数字是相对随机的,但是下⼀次运⾏程序⽣成的结果和上⼀次⼀模⼀样,这就说明有点问题。
如果再深⼊了解⼀下,我们就不难发现, 其实rand函数生成的随机数是伪随机的,伪随机数不是真正的随机数,是通过某种算法生成的随机数。真正的随机数的是无法预测下⼀个值是多少的。而rand函数是对⼀个叫“种⼦”的基准值进⾏运算⽣成的随机数。
之所以前⾯每次运⾏程序产⽣的随机数序列是⼀样的,那是因为rand函数⽣成随机数的默认种⼦是1。 如果要⽣成不同的随机数,就要让种⼦是变化的。
int=rand(2)
int=rand(3)
int=rand(4)
......

2.srand

C语言中又提供了⼀个函数叫 srand,用来初始化随机数的生成器的,srand的原型如下:

void srand (unsigned int seed)
程序中在调⽤ rand 函数之前先调⽤ srand 函数,通过 srand 函数的参数seed来设置rand函数⽣成随 机数的时候的种⼦,只要种⼦在变化,每次⽣成的随机数序列也就变化起来了。
那也就是说给srand的种⼦是如果是随机的,rand就能⽣成随机数;在⽣成随机数的时候⼜需要⼀个随机数,这就⽭盾了。

3.time

在程序中我们⼀般是使⽤程序运⾏的时间作为种⼦的,因为时间时刻在发⽣变化的。 在C语⾔中有⼀个函数叫time   ,就可以获得这个时间,time函数原型如下:
time_t time (time_t* timer);
time 函数会返回当前的⽇历时间,其实返回的是1970年1⽉1⽇0时0分0秒到现在程序运⾏时间之间的差值,单位是秒。返回的类型是time_t类型的,time_t 类型本质上其实就是32位或者64位的整型类型。

time函数的参数 timer 如果是⾮NULL的指针的话,函数也会将这个返回的差值放在timer指向的内存中带回去。
如果 timer 是NULL,就只返回这个时间的差值。time函数返回的这个时间差也被叫做 时间戳 time函数的时候需要包含头⽂件:time.h
//VS2022 上time_t类型的说明
#ifndef _CRT_NO_TIME_T
  #ifdef _USE_32BIT_TIME_T
      typedef __time32_t time_t;
  #else
      typedef __time64_t time_t;
  #endif
#endif

typedef                    long __time32_t;
typedef                    __int64 __time64_t
如果只是让time函数返回时间戳,我们就可以这样写:
time(NULL);//调⽤time函数返回时间戳,这⾥没有接收返回值
那我们就可以让⽣成随机数的代码改写成如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
  //使⽤time函数的返回值设置种⼦
  //因为srand的参数是unsigned int类型,我们将time函数的返回值强制类型转换
  srand((unsigned int)time(NULL)); //放主函数只执行一次
  printf("%d\n", rand());
  printf("%d\n", rand());
  printf("%d\n", rand());
  printf("%d\n", rand());
  printf("%d\n", rand());
  return 0;
}

(注:截图只是当时程序运行的结果,你的运行结果不⼀定和这个⼀样) 

srand函数是不需要频繁调⽤的,⼀次运行的程序中调用⼀次就够了

4.设置随机数的范围

如果我们要⽣成0~99之间的随机数,⽅法如下:
 rand() %100;//余数的范围是0~99
如果要⽣成1~100之间的随机数,⽅法如下:
rand()%100+1;//%100的余数是0~99,0~99的数字+1,范围是1~100
如果要⽣成100~200的随机数,方法如下:
100 + rand()%(200-100+1)
   //余数的范围是0~100,加100后就是100~200
所以如果要生成a~b的随机数,⽅法如下:
a + rand()%(b-a+1)

二.猜数字游戏实现

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
 int r = rand()%100+1;
 int guess= 0;
 while(1)
 {
   printf("请猜数字>:");
   scanf("%d", &guess);
   if(guess < r)
   {
      printf("猜⼩了\n");
   }
   else if(guess > r)
   {
      printf("猜⼤了\n");
   }
   else
   {
     printf("恭喜你,猜对了\n");
     break;
   }
 } 
}
void menu()
{
 printf("***********************\n");
 printf("****** 1. play ******\n");
 printf("****** 0. exit ******\n");
 printf("***********************\n");
}
int main()
{
 int input = 0;
 srand((unsigned int)time(NULL));
 do
 {
   menu();
   printf("请选择:>");
   scanf("%d", &input);
   switch(input)
 {
   case 1:
     game();
     break;
   case 0:
     printf("游戏结束\n");
     break;
   default:
     printf("选择错误,重新选择\n");
     break;
 }
 }while(input);
 return 0;
}
还可以加上猜数字的次数限制,如果5次猜不出来,就算失败.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
  int r = rand() % 100 + 1;
  int guess = 0;
  int count = 5;
  while (count) //=0跳出循环
  {
     printf("\n你还有%d次机会\n", count);
     printf("请猜数字>:");
     scanf("%d", &guess);
     if (guess < r)
     {
       printf("猜⼩了\n");
     }
     else if (guess > r)
     {
       printf("猜⼤了\n");
     }
     else
     {
       printf("恭喜你,猜对了\n");
       break;
     }
 count--;
 }
 if (count == 0)
 {
 printf("你失败了,正确值是:%d\n", r);
 }
}
void menu()
{
 printf("***********************\n");
 printf("****** 1. play ******\n");
 printf("****** 0. exit ******\n");
 printf("***********************\n");
}
int main()
{
 int input = 0;
 srand((unsigned int)time(NULL));
 do
 {
   menu();
   printf("请选择:>");
   scanf("%d", &input);
 switch (input)
 {
  case 1:
    game();
    break;
  case 0:
    printf("游戏结束\n");
    break;
  default:
   printf("选择错误,重新选择\n");
   break;
  }
 } while (input);
 return 0;
}

相关文章:

  • Guitar Pro 8.1中文版永久许可证激活2024最新24位注册激活码生成器
  • 开发Chrome扩展插件
  • Springboot + Vue用户管理系统
  • 鸿蒙开发之gson解析
  • Web自动化测试之selenium环境搭建
  • Zynq—AD9238数据采集DDR3缓存千兆以太网发送实验(二)
  • linux防火墙和开放端口命令
  • MySQL--MHA高可用方案
  • 宏auto关键字(C++基础)
  • GPT-4技术解析:与Claude3、Gemini、Sora的技术差异与优势对比
  • linux循环之until循环
  • 2023年度总结报告
  • 【并查集】一种简单而强大高效的数据结构
  • Spring Boot 生成与解析Jwt
  • Qt区分左右Shift按键
  • Python爬虫——Urllib库-3
  • 基于chatgpt的聊天机器人
  • 如何保证 HTTPS 证书的有效性?
  • SpringCloud2023最新版本该如何进行组件选型?
  • 飞桨(PaddlePaddle)Tensor使用教程
  • 专访|高圆圆:像鸟儿一样,柔弱也自由
  • 常州市委原常委、组织部部长陈翔调任江苏省民宗委副主任
  • “用鲜血和生命凝结的深厚情谊”——习近平主席署名文章中的中俄友好故事
  • 上任后首访,德国总理与法国总统举行会晤
  • 中邮保险斥资8.69亿元举牌东航物流,持股比例达5%
  • 黄仁勋:中国AI市场将达500亿美元,美国企业若无法参与是巨大损失