[英]The rand () function generates numbers in the wrong range
我猜數字游戲有問題。 該程序由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(含)的數字。 然后加上a
會產生一個從a
到a
+ b
-1 的數字,包括。 所以,如果a
是 200, b
是 800,它會產生一個從 200 到 200+800−1 = 999(含)的數。
要生成從a
到b
(含),請使用a + rand() % (b+1-a)
。 或者,要排除b
,請使用a + rand() % (ba)
。
注意:
(a) 隨機使用%
會使分布偏向低值。 這是因為rand
產生的數字范圍(0 到RAND_MAX
,包括)通常不能被b
整除,所以有一些剩余片段,使用%
將這個片段中的所有數字放在分布的低端。
(b) rand
歷史實現因低位的低熵而臭名昭著,因此,只要b
是 2 的冪的倍數,例如 2 n , n低位就不是很隨機。
出於這些原因,以這種方式將%
與rand
一起使用不應在簡單的課堂練習和其他隨意使用之外進行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.