Extraño comportamiento srand en el interior del bucle; c ++
Simplemente escribiendo un programa para barajar un mazo de cartas, y obtuve un comportamiento diferente dependiendo de si el RNG fue sembrado dentro o fuera del bucle for; es decir.
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;
}
da la salida
Nine of Hearts
Ace of Clubs
Two of Clubs
Three of Clubs
Four of Clubs
etc, pero
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;
}
lleva a
Ace of Hearts
Queen of Spades
Four of Hearts
Seven of Clubs
Five of Hearts
(La funcionalidad correcta). Alguien sabe por qué volver a sembrar el RNG causaría esto?