0

I'm having some issues getting a random int. I need to run this function in a for loop, but every time I run the program does generate a random output until the next time i run it. Sometimes even running multiples times the output is the same.

char gerarMesa(){
          srand(time(NULL));
          int n = rand() % 5;
          return (char) n + 65;
 }

terminal output

1 Answers1

0

Because you are probably calling this function in a tight loop, it's possible that your time function is returning the same result for each call to gerarMesa. Since you are re-initializing the seed with the same number each time you request a random number, you are getting the same result.

Try only calling srand once, at the beginning of your main function.

Will Bradley
  • 1,733
  • 15
  • 27