i can’t randomly generate variables in an interval even if i have to be on an interval

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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top