mardi 2 novembre 2021

While loop in C which genrates random numbers running for infinity

I want to make a program, which generates 2 random coordinates and then it tests if the place in the 2D array I made before is empty (the value in the array is 0). If true, it places value 2 to that coordinates, else it should keep looking for such place. The issue is that it is running for infinity in some cases.

Here is the array I made:

int array[4][4] =
{
    {0,0,0,0},
    {0,0,0,0},
    {0,0,0,0},
    {0,0,0,0}
};

Here is the rest of the code:

srand(time(NULL));
int ranadom_num[2] = { rand() % 4, rand() % 4 };
while (array[ranadom_num[0]][ranadom_num[1]] != 0)
{
    int ranadom_num[2] = { rand() % 4, rand() % 4 };
}
array[ranadom_num[0]][ranadom_num[1]] = 2;

Any help? :)




Aucun commentaire:

Enregistrer un commentaire