分支和循环(下)

目录

1. 随机数生成

1.1 rand

1.2 srand

1.3 time

1.4 设计随机数的范围 

2. 猜数字游戏实现


写一个猜数字游戏

游戏要求:

  1. 电脑自动生成1~100的随机数
  2. 玩家猜数字,猜数字的过程中,根据猜测数据的大小给出打了或小了的反馈, 直到猜对了,游戏结束。

1. 随机数生成

1.1 rand

C语言提供了一个函数叫rand,这函数是可以生成随机数的。

int rand(void);

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

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

那么我们就可以调用rand函数,看看它生成的随机数。

#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。 如果要⽣成不同的随机数,就要让种子是变化的。

1.2 srand

 C语言中又提供了一个函数叫srand,用来初始化随机数的生成器。

void srand(unsigned int seed);

程序中在调⽤rand函数之前先调用srand函数,通过srand函数的参数seed来设置rand函数⽣成随 机数的时候的种子,只要种⼦在变化,每次生成的随机数序列也就变化起来了。

那也就是说给srand的种⼦是如果是随机的,rand就能⽣成随机数;在生成随机数的时候⼜需要⼀个随机数,这就矛盾了。

1.3 time

在程序中我们一般是使用程序运行的时间作为种子的,因为时间时刻在发生变化。

在C语言中有一个函数叫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

//VS2019 上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

// Indicate that these common types are defined
#ifndef _TIME_T_DEFINED
    #define _TIME_T_DEFINED
#endif

#if __STDC_WANT_SECURE_LIB__
    typedef size_t rsize_t;
#endif

 如果只想让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函数是不需要频繁调用的,一次程序调用一次就够了。

1.4 设计随机数的范围 

生成0~99之间的随机数。

rand() % 100;//余数的范围是0~99

生成1~100之间的随机数。

rand() % 100 + 1;//余数是0~99,加1就变成1~100

生成100~200的随机数。

100 + rand() % (200-100+1);//余数的范围是0~100,加100就是100~200

生成a~b的随机数。

a + rand() % (b - a + 1);

2. 猜数字游戏实现

代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void show()
{
	printf("****************************\n");
	printf("***********1.play***********\n");
	printf("***********2.exit***********\n");
	printf("****************************\n");
}
void play()
{
	int index = rand() % 100 + 1;
	int value = 0;
	printf("请输入你要猜的数字(1~100):");
	while (1)
	{
		scanf("%d", &value);
		if (value < index)
			printf("猜小了\n");
		else if (value > index)
			printf("猜大了\n"); 
		else
		{
			printf("猜对了!!!\n");
			break;
		}
	}
}
int main()
{
	int input = 0;
	srand(time(NULL));
	do
	{
		show();
		printf("请输入序号:");
		scanf("%d", &input);
		switch (input)
		{
		case 2:
			printf("退出游戏!!!\n");
			break;
		case 1:
			play();
			break;
		default:
			printf("输入错误,请重新输入!!!\n");
			break;
		}
	} while (input != 2);
	return 0;
}

运行: 

我们还可以限制猜数字的次数,如果5次都没猜中,就说明游戏失败。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void show()
{
	printf("****************************\n");
	printf("***********1.play***********\n");
	printf("***********2.exit***********\n");
	printf("****************************\n");
}
void play()
{
	int index = rand() % 100 + 1;
	int value = 0;
	int a = 5;//游戏限制次数
	printf("请输入你要猜的数字(1~100):\n");
	while (a)
	{
		printf("还有%d次机会:>", a);
		scanf("%d", &value);
		if (value < index)
			printf("猜小了\n");
		else if (value > index)
			printf("猜大了\n"); 
		else
		{
			printf("猜对了!!!\n");
			break;
		}
		a--;
	}
	if (a == 0)
	{
		printf("5次机会已经完了,游戏失败!!!\n");
		printf("本次随机数是%d\n", index);
	}
}
int main()
{
	int input = 0;
	srand(time(NULL));
	do
	{
		show();
		printf("请输入序号:");
		scanf("%d", &input);
		switch (input)
		{
		case 2:
			printf("退出游戏!!!\n");
			break;
		case 1:
			play();
			break;
		default:
			printf("输入错误,请重新输入!!!\n");
			break;
		}
	} while (input != 2);
	return 0;
}

运行:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值