Home > database >  What should I use to check if user's input is the same as generated random numbers?
What should I use to check if user's input is the same as generated random numbers?

Time:11-27

My code generates 5 random numbers and I want the user to guess these numbers after 5 seconds of flashing it.

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

main()
{
    menuchoose1();
    
}

int menuchoose1(){
    int menu1choose;
    int score=0;
    int mode,i,j;
    int n=5;
    printf("1.Continuous Mode\n");
    printf("2.Stage Mode\n");
    scanf("%d",&menu1choose);
    
    switch(menu1choose){
    int answers;
            case 1:
                srand(time(NULL)* getpid() );
                int a[5];
                unique(a,5,10,99);
                int i;
                printf("You have 5 seconds to remember these numbers\n");               
                for(i=0;i<5;i  )
                printf("%d\t",a[i]);
                sleep(5);
                system("cls");
                scanf("%d",&answers);
                if(answers==a[i]){
                    printf("Correct");
                }else
                    printf("Incorrect");
                break;
        }
    return;
}

void unique(int array[], int length, int min, int max){
    
    int new_randomnum;
    bool unique;
    int i;
    for(i=0;i<length;i  ){
        do{
            new_randomnum = (rand()%(max - min   1 ))   min;
            unique = true;
            int j;
            for(j=0;j<0;j  )
            if(array[j] == new_randomnum) unique = false;
        }while(!unique);
        array[i] = new_randomnum;
    }
}

I've tried using scanf but it always ends up incorrect and generating it one by one then checking it one by one would be inefficient.

CodePudding user response:

First off, this program produces a ton of compiler warnings. I strongly suggest you get in the habit of enabling warnings, and never ignore them! For instance, you are missing a lot of headers and forward declarations. If your compiler does not give you those warnings, then you should consider switching to a better one.

Below I will only mention those bugs that do not trigger warnings.

       srand(time(NULL)* getpid() );

If you call srand on each round of the game, you risk getting the same sequence of numbers again. Just call it once at the start of the program, e.g. in main.

            for(i=0;i<5;i  )
                printf("%d\t",a[i]);
            sleep(5);
            system("cls");
            scanf("%d",&answers);
            if(answers==a[i]){
                printf("Correct");
            }else
                printf("Incorrect");
            break;

I fixed the indentation to emphasize the bug. The if(answers == a[i]) is after the loop and therefore i always has the value 5 when it is reached, which is out of bounds for the array a, causing undefined behavior. You probably want the code to get and check the user's answer to be in a loop of its own.

        for(j=0;j<0;j  )
            if(array[j] == new_randomnum)
                unique = false;

The j<0 condition means this loop will never execute. You probably meant j<i.

CodePudding user response:

Detail on one part of OP's code.

srand(time(NULL)* getpid() ); is a weak way to initialize the random number generator.

Example: if either of the least significant bits of time(NULL) or getpid() are 0, the product with have a least significant bit of zero. Thus that bit has a 75% change of being 0.

Instead, call srand() once, before the loops and use the 2 function calls like: (unsigned) time(NULL) ^ getpid(). By using ^, the bits will not get biased as discussed above.

  •  Tags:  
  • c
  • Related