0

With this program I tried to make the computer guess the number I'm thinking. For example I choose x=50,y=50 and the program will try to guess it.My problem here is that the number the computer is guessing is out of the interval, for example the interval is between 1,100 and it will guess x=90 y=120. I have tried many ways to go around the code to try to understand why this is happening but I can't find the problem... If anyone knows why the computer isn't guessing in my interval it would be great!!

int main(void){
    char tip[2];
    int xmax=100,ymax=100;
    int xmin=1,ymin=1;
    int x,y,a=0;
    while(true){
        srand( (unsigned)time(NULL));
        while (x>xmax && x<xmin && y>ymax && y<ymin){
                x=rand()%xmax+xmin;
                y=rand()%ymax+ymin;
        }

        printf("This is my try: x=%d e y=%d\nTip?\n",x,y);
        scanf("%s", tip);
        if((tip[0]=='E'||tip[0]=='W'||tip[0]=='N'|| tip[0]=='S')&& (tip[1]=='W'||tip[1]=='E'||tip[1]=='N'||tip[1]=='S'||tip[1]=='\0')){
            if (tip[0]=='N'){
                ymin=y;
            
            }
            else if(tip[0]=='S'){
                    ymax=y;
                
            }
            else if(tip[0]=='E'){
                xmin=x;
                
            }
            else if(tip[0]=='W'){
                xmax=x;
                    
            }
            if(tip[1]=='W'){
                xmax=x;
                
            }
            else if(tip[1]=='E'){
                xmin=x;
                
            }
            else if(tip[1]=='N'){
                ymin=y;
                
            }   
            else{
                ymax=y;
                
            }       
                
        }
    }
    return 0;
}
  • 1
    It is surely not right to reseed the random number generator on every iteration of the loop. If your program takes less than one second to run, you will get the same pair of numbers every time. You may want to study more carefully how RNG seeding works. – Nate Eldredge Nov 04 '20 at 04:44
  • 1
    Also, `x=rand()%xmax+xmin` doesn't generate a random number between `xmin` and `xmax`. Think about what it does instead. – Nate Eldredge Nov 04 '20 at 04:45
  • You are calling `srand` in a loop, [this is wrong](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – n. m. could be an AI Nov 04 '20 at 04:49

1 Answers1

0

As already mentioned in the comments, place the srand() call before the main while(True) loop.

And replace the inside range checking while loop with these below. Your while loop will never be executed because x > xmax and x < xmin is not satisfiable simultaneously given xmin < xmax. Also it can happen that one of x and y is out of bounds. So better use 2 separate range checking loops.

...
if(x > xmax || x < xmin)
   x = xmin + rand() % (xmax - xmin + 1);

if(y > ymax || y < ymin)
   y = ymin + rand() % (ymax - ymin + 1);
...

Also note that there is no need to put those range checks inside a while loop, a simple if check suffices.

swag2198
  • 2,546
  • 1
  • 7
  • 18
  • I appreciate your words but please take a look here: https://stackoverflow.com/help/someone-answers. – swag2198 Nov 04 '20 at 12:06