首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >分支和循环(下)

分支和循环(下)

原创
作者头像
四念处茫茫
发布2025-02-01 13:14:15
发布2025-02-01 13:14:15
2790
举报
文章被收录于专栏:C语言C语言

一、 随机数生成

1、随机数生成

例子:写一个猜数字游戏! 游戏要求: 1、电脑自动生成1~100的随机数。 2、玩家猜数字,猜数字的过程中,根据猜测数据的大小给出大了或小了的反馈,直到猜对,游戏结束。

1.1 rand

rand函数原型:

代码语言:c
复制
int rand (void);

举例引入rand函数:

代码语言:c
复制
 #include <stdio.h>
 #include <stdlib.h>
 int main()
 {
 printf("%d\n", rand());//41
 printf("%d\n", rand());//18467
 printf("%d\n", rand());//6334
 printf("%d\n", rand());//26500
 printf("%d\n", rand());//19169
 return 0;
 }

两次输出的结果都一样。

rand函数会生成一个伪随机数,范围:0~RAND_MAX rand函数的使用需要包含一个头文件是:&lt;stdlib.h&gt;。 伪随机数不是真正的随机数,是通过某种算法生成的随机数。真正的随机数的是无法预测下一个值是多少的,而rand函数是对一个叫“种子”的基准值进行运算生成的随机数。 之所以前面每次运行程序产生的随机数序列是一样的,那是因为rand函数生成随机数的默认种子是1。

1.2 srand

srand函数原型:

代码语言:c
复制
 void srand (unsigned int seed);

如果要生成不同的随机数,就要让种子是变化的,用srand( ),srand函数要在rand之前调用。 srand和rand都要包含头文件&lt;stdlib.h&gt;。 通过srand函数的参数seed来设置rand函数⽣成随机数的时候的种⼦,只要种⼦在变化,每次⽣成的随机数序列也就会变化,所以只要使srand种子是随机的就会让rand生成的数是随机的。

代码语言:c
复制
#include <stdio.h>
#include <stdlib.h>
int main()
{
srand(2);//这里srand的函数原型是:viod srand(unsigned int seed);
printf("%d\n",rand());
return 0;
}

那么如何才能使srand的种子变成一个随机的呢?那么下面我们引入“time”及时间戳。

1.3 time

为何要以时间作为种子呢? 在程序中我们一般是使用程序运行的时间作为种子的,因为时间时刻都在变化,我们所需要的种子正是时刻变化的。 在C语言中有一个函数叫time,就可以获得这个时间,time函数的原型如下:

代码语言:c
复制
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

time函数返回时间戳:

代码语言:c
复制
time(NULL);

有了以上的理解,我们重新写一段代码实现随机数的生成:

代码语言:c
复制
 #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函数是不需要频繁调⽤的,⼀次运⾏的程序中调⽤⼀次就够了。

1.4设置随机数范围

如果要生成a~b的随机数: a + rand( ) % (b - a + 1)

二、猜数字游戏的实现

通过前面的循环嵌套以及上面所学的内容等来编写猜数字游戏:

代码语言:c
复制
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
    int r = rand() % 100 + 1;
    int guess = 0;
    int count = 5;
    while (1)
    {
        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);
            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;
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、 随机数生成
    • 1、随机数生成
      • 1.1 rand
      • 1.2 srand
      • 1.3 time
      • 1.4设置随机数范围
  • 二、猜数字游戏的实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档