繁体   English   中英

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

[英]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会产生一个从aa + b -1 的数字,包括。 所以,如果a是 200, b是 800,它会产生一个从 200 到 200+800−1 = 999(含)的数。

要生成从ab (含),请使用a + rand() % (b+1-a) 或者,要排除b ,请使用a + rand() % (ba)

注意:

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

(b) rand历史实现因低位的低熵而臭名昭著,因此,只要b是 2 的幂的倍数,例如 2 nn低位就不是很随机。

出于这些原因,以这种方式将%rand一起使用不应在简单的课堂练习和其他随意使用之外进行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM