Just writing a program to shuffle a deck of cards, and got different behaviour depending on whether the RNG was seeded inside or outside of the for loop; ie.
for(int i = 0; i < 52; i++)
{
srand(time(0));
Card temp = deck[i];
int toSwap = rand()%52;
deck[i] = deck[toSwap];
deck[toSwap] = temp;
}
gives the output
Nine of Hearts
Ace of Clubs
Two of Clubs
Three of Clubs
Four of Clubs
etc, but
void DeckOfCards::shuffle()
{
srand(time(0));
for(int i = 0; i < 52; i++)
{
Card temp = deck[i];
int toSwap = rand()%52;
deck[i] = deck[toSwap];
deck[toSwap] = temp;
}
currentCard =0;
}
leads to
Ace of Hearts
Queen of Spades
Four of Hearts
Seven of Clubs
Five of Hearts
(the correct functionality). Anyone know why reseeding the RNG would cause this?
As time(NULL) changes only every second, the RNG seed will be the same if the for-loop doesn't take more than a second to complete.