mardi 18 février 2020

Generating the same random number when using a random number function multiple times in C [duplicate]

This function to generate a random int in a given range works but when I call it with the same range multiple times in a row, the same number is generated (in the correct range however) every time. For example I call it for 0-99 and it works. Then i need to call it multiple times in a row for the same range and the same int is returned every time. I'm not sure why this is happening and any help is appreciated.

//generate random integer within parameter range
int randomInRange(int min, int max)
{
  srand(time(NULL));
  int num =  (rand() % (max - min + 1)) + min;
  return num;
}
for(i=0; i<roundAmount; i++){
            gamePlayed = randomInRange(0,3);
            printf("%d\n", gamePlayed);
        }



Aucun commentaire:

Enregistrer un commentaire