0

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);
        }
  • 1
    Only call `srand()` once — preferably altogether but definitely per session in a program. – Jonathan Leffler Feb 19 '20 at 01:24
  • Does this answer your question? [using rand to generate a random numbers](https://stackoverflow.com/questions/3159644/using-rand-to-generate-a-random-numbers) – Felix Jassler Feb 19 '20 at 01:25

0 Answers0