vendredi 1 mai 2020

I am getting same value for every time in rand() in C

in this program i need to take 2 random number and one of the side must be equal to 3, to make them randomSet function works with while loop, but the problem is in the main function, the loop gives me same 2 number every time, how to i get different numbers each iteration.

int randomSet(int temp[])
{
  while (temp[0] != 3 && temp[1] != 3)
         {
            temp[0] =rand()%4;
            temp[1] =rand()%4;
         }
}

the main function is

int main()
{
srand(time(0));
int temp[2];
int x =0,y =0;
for (int i = 0; i <5; i++) {
    randomSet(temp);
}
    return 0;
}



Aucun commentaire:

Enregistrer un commentaire