1

Function rand_char is used to generate random letters that will be assigned to the string.

char rand_char(){
    char rand_ch;
    srand(time(NULL));
    rand_ch = rand() %26 +'A';
    return rand_ch;
}
for (j=0; j<7; j++){
    game->players[0].hand[j].letter = rand_char();
}

if I check printf("%c",game->players[0].hand[j].letter); all elements have the same letter. But they should have different random letters.

  • 1
    Also a duplicate of [`srand()` — why call it just once?](http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once/) – Jonathan Leffler Dec 28 '16 at 03:46

1 Answers1

3

You should move srand(time(NULL)); out of rand_char(). srand should be done once before the for loop, not every time in rand_char()

artm
  • 17,291
  • 6
  • 38
  • 54