只需编写一个程序来洗牌,并根据 RNG 是在 for 循环内部还是外部播种而得到不同的行为;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;
}
给出输出
Nine of Hearts
Ace of Clubs
Two of Clubs
Three of Clubs
Four of Clubs
等等,但是
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;
}
导致
Ace of Hearts
Queen of Spades
Four of Hearts
Seven of Clubs
Five of Hearts
(正确的功能)。任何人都知道为什么重新播种 RNG 会导致这种情况?