简体   繁体   中英

The rand () function generates numbers in the wrong range

I have a problem in my guess the number game. The program consists of 2 parts. In the first part, the user tries to guess the number randomly generated by the computer. In part 2, the computer tries to estimate the number entered by the user. There is no problem in part 1, but in part 2, the computer estimates the values that do not provide ranges when making predictions. So, I used the variables a and b to create numbers between 1 and 1000 in the rand () function and defined it as a = 1 b = 1000. The computer constantly narrows this range according to the user's answers.

For example, the user has entered the number 649. If the computer estimates 800, the relevant part is working and the new value of b is 800, not 1000, so the upper limit is updated. If the computer estimates 200, the relevant part is working again and the new value of a is 200, and when the loop wraps around, a number between 200 and 800 is produced, not between 1 and 1000. But the problem is that even though the values are updated, the computer generates a number higher than 800 (for example, 1246). That's exactly the problem.

I have to deliver this assignment on Tuesday, but I still have not been able to find a solution to this error. I am sending you the source code and I wrote down in detail how each variable I use works as a comment number. If you want, I can send the video description of the error. Please help me.

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.

/*
                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;
}

In a + rand() % b; , rand() % b results in a number from 0 to b −1, inclusive. Then adding a produces a number from a to a + b −1, inclusive. So, if a is 200 and b is 800, it produces a number from 200 to 200+800−1 = 999, inclusive.

To produce a number from a to b , inclusive, use a + rand() % (b+1-a) . Or, to exclude b , use a + rand() % (ba) .

Note that:

(a) Using % with random skews the distribution toward low values. This is because the range of numbers produced by rand (0 to RAND_MAX , inclusive) is generally not divisible by b , so there is some residual fragment, and using % puts all the numbers in this fragment at the low end of the distribution.

(b) Historic implementations of rand are notorious for low entropy in the low bits, so, whenever b is a multiple of a power of two, say 2 n , the n low bits are not very random.

For these reasons, using % with rand in this way should not be done outside of simple class exercises and other casual uses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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