gpt4 book ai didi

c++ - rand() 函数生成错误范围内的数字

转载 作者:行者123 更新时间:2023-12-01 12:59:08 25 4
gpt4 key购买 nike

我猜数字游戏有问题。该程序由2部分组成。在第一部分,用户尝试猜测计算机随机生成的数字。在第 2 部分,计算机尝试估计用户输入的数字。第 1 部分没有问题,但在第 2 部分,计算机在进行预测时会估计不提供范围的值。所以,我在rand()函数中使用变量a和b创建了1到1000之间的数字,并定义为a = 1 b = 1000。计算机根据用户的回答不断缩小这个范围。

例如,用户输入了数字649,如果计算机估计为800,则相关部分正在工作,并且b的新值是800,而不是1000,因此更新上限。如果计算机估计为 200,则相关部分再次工作,a 的新值是 200,当循环回绕时,产生的是 200 到 800 之间的数字,而不是 1 到 1000 之间的数字。 但问题是即使值更新后,计算机生成一个大于 800 的数字(例如,1246)。这正是问题所在。

我必须在星期二交付这项作业,但我仍然无法找到解决此错误的方法。我正在向您发送源代码,并详细写下了我使用的每个变量如何作为注释编号。如果需要,我可以发送错误的视频描述。请帮我。

为了帮助您更快地查找和测试问题,我禁用了一些带有注释行向用户提问的行。

/*
VARIABLES

a= rand() fonction min limit.
b= rand() fonction max limit.
i= attempts of user
j= attempts of computer
rnd1= The computer generated number that the user is trying to find.
rnd2= The number generated by the computer when estimating the number entered by the user and changing in each cycle.
number2= The number the computer is trying to find.
number1= Each number that the user enters when trying to find the number created by the computer.
input= The value that takes the answer to the questions the computer asks the user.
restart= The value that is defined to 1 and allows the game to restart unless the user changes the value.

ATTENTION: In order to help you find and test the problem faster, I disabled some
lines where the user was asked questions with a comment line.
*/

#include "stdlib.h"
#include <stdio.h>
#include <dos.h>
#include <windows.h>
#include <locale.h>

int main()
{
int i, j = 1, rnd1, rnd2, number2 = 0, number1 = 0, b = 1000, a = 1, input, restart = 1;

setlocale(LC_ALL, "Turkish");

do
{
printf("\t\t\t\t-----------------------------------------\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t| Welcome to Guess Number Game |\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t-----------------------------------------\n");

printf("Game is starting...\n");
printf("3\n");
Sleep(1000);
printf("2\n");
Sleep(1000);
printf("1");
Sleep(1000);
system("cls");

srand(time(NULL));
rnd1 = a + rand() % b;

printf("Computer: Let's try to guess the number that in my mind :)\n\n");
for (i = 1; number1 != rnd1; i++)
{
printf("Computer: What is your guess?\n");
printf("Number: ");
scanf_s("%d", &number1);
printf("\n");

if (number1 < rnd1)
{
printf("Computer: The secret number is greater than your guess. Try again...\n");
}
else if (number1 > rnd1)
{
printf("Computer: The secret number is smaller than your guess. Try again...\n");
}
else
{
printf("Computer: Congratulations!!! You know the number at your %d’th guess\n\n\n", i);
printf("\t\t\t\t------------------------------------------\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t| Computer's Turn... |\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t------------------------------------------\n");

printf("3\n");
Sleep(1000);
printf("2\n");
Sleep(1000);
printf("1");
Sleep(1000);
system("cls");
}
}
printf("Computer: Now, its my turn. Write a number and I'll try to find it...\n");
printf("Number: ");
scanf_s("%d", &number2);
printf("\n");

while (j != -1)
{
rnd2 = a + rand() % b;
if (rnd2 == number2)
{
printf("Computer: My guess is %d. Is this true ?\n", rnd2);
//printf("1)Yes. It is equal to the number which is in my mind.\n");
//printf("2)No. It is greater than to the number which is in my mind.\nNumber: ");
//printf("2)No. It is smaller than to the number which is in my mind.\nNumber: ");
//scanf_s("%d", &input);
//printf("\n\n");
//if (input == 1)
//{
// printf("Computer: I found! I found!. The secret number is %d.\n\n", rnd2);
//}
//else if (input == 2)
//{
// printf("Don't try to fool me, I'm a computer. I know it's true :)\n");
//}
//else if (input == 3)
//{
// printf("Don't try to fool me, I'm a computer. I know it's true :)\n");
//}
break;
}
else if (rnd2 < number2)
{
printf("Computer: My guess is %d. Is this true ?\n", rnd2);
//printf("1)Yes. It is equal to the number which is in my mind.\n");
//printf("2)No. It is greater than to the number which is in my mind.\nNumber: ");
//printf("2)No. It is smaller than to the number which is in my mind.\nNumber: ");
//scanf_s("%d", &input);
//printf("\n\n");
//if (input == 1)
//{
// printf("Don't try to fool me, I'm a computer. I know it's smaller than yours :)\n");
//}
//else if (input == 2)
//{
// printf("Don't try to fool me, I'm a computer. I know it's smaller than yours :)\n");
//}
//else if (input == 3)
//{

//}
a == rnd2;
}
else if(rnd2>number2)
{
printf("Computer: My guess is %d. Is this true ?\n", rnd2);
//printf("1)Yes. It is equal to the number which is in my mind.\n");
//printf("2)No. It is greater than to the number which is in my mind.\nNumber: ");
//printf("2)No. It is smaller than to the number which is in my mind.\nNumber: ");
//scanf_s("%d", &input);
//printf("\n\n");
//if (input == 1)
//{
// printf("Don't try to fool me, I'm a computer. I know it's greater than yours :)\n");
//}
//else if (input == 2)
//{

//}
//else if (input == 3)
//{
// printf("Don't try to fool me, I'm a computer. I know it's greater than yours :)\n");
//}
b == rnd2;
}
j++;
}

if (i < j)
{
printf("Game is ending...\n");
printf("3\n");
Sleep(1000);
printf("2\n");
Sleep(1000); //
printf("1");
Sleep(1000);
system("cls");

printf("\t\t\t\t------------------------------------------\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t| You are the winner. CONGRATULATIONS. |\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t------------------------------------------\n");
printf("You know the number at your %d’th guess\n", i);
printf("Computer know the number at %d’th guess\n\n", j);
printf("Do you want to play again?\n");
printf("1)Yes\n");
printf("2)No\nSelection:");
scanf_s("%d", &restart);

}
else if (i > j)
{
printf("Game is ending...\n");
printf("3\n");
Sleep(1000);
printf("2\n");
Sleep(1000); //Game end countdown.
printf("1");
Sleep(1000);
system("cls");
printf("\t\t\t\t-----------------------------------------\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t| Computer is the winner. You lose. |\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t-----------------------------------------\n");
printf("You know the number at your %d’th guess\n", i);
printf("Computer know the number at %d’th guess\n\n", j);
printf("Do you want to play again?\n");
printf("1)Yes\n");
printf("2)No\nSelection:");
scanf_s("%d", &restart);
}
else
{
printf("Game is ending...\n");
printf("3\n");
Sleep(1000);
printf("2\n");
Sleep(1000); //Game end countdown.
printf("1");
Sleep(1000);
system("cls");
printf("\t\t\t\t-----------------------------------------\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t| Your match ended in a draw. Do you want to play again? |\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t-----------------------------------------\n");
printf("You know the number at your %d’th guess\n", i);
printf("Computer know the number at %d’th guess\n\n", j);
printf("Do you want to play again?\n");
printf("1)Yes\n");
printf("2)No\nSelection:");
scanf_s("%d", &restart);
}
} while (restart == 1);

system("PAUSE");
return 0;
}

最佳答案

a + rand() % b; , rand() % b结果是从 0 到 b 的数字-1,包括在内。然后添加 aa 产生一个数字至 a + b -1,包括在内。所以,如果 a是 200 和 b是 800,它产生一个从 200 到 200+800−1 = 999(含)的数字。

a 产生一个数字至 b ,包括,使用 a + rand() % (b+1-a) .或者,排除 b , 使用 a + rand() % (b-a) .

注意:

(a) 使用 %随机使分布偏向低值。这是因为 rand 产生的数字范围(0 到 RAND_MAX ,含)通常不能被 b 整除,所以有一些残留片段,并使用 %将这个片段中的所有数字放在分布的低端。

(b) rand 的历史实现以低位的低熵而臭名昭著,因此,每当 b是 2 的幂的倍数,比如 2n,n 个低位不是很随机。

由于这些原因,使用 %rand这种方式不应该在简单的类练习和其他随意使用之外进行。

关于c++ - rand() 函数生成错误范围内的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60694911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com