これは偶然のような素晴らしい機能なので、「カード」の配列をシャッフルするのは難しいことです。私が同じ番号を取得しているという事実は、毎回別の種子を選ぶ際に問題があることを示しています。 srand48
またはtime(NULL)
コールが不適切ですか?私は行方不明のいくつかの根本的な論理欠陥がありますか? time()
の値が異なるように繰り返し間に十分な時間がないのですか?私のC乱数ジェネレータはなぜ "42"だけを返しますか?
コードはLinuxで実行されています。
void shuffle()
{
int i_rnd; /* Integer random number, range 0..100 */
int i_rnd2;
card tempCard; /*temporary card to facillitate swapping*/
int i = 0; /*can't use a FOR loop 'cause we're not using c99 standard*/
while(i < 1000)
{
srand48((unsigned) time(NULL)); /* Seed the random number generator */
i_rnd = (int) (drand48() * 100);
i_rnd = i_rnd%52; // return a random number 0-51
i_rnd2 = (int) (drand48() * 100);
i_rnd2 = i_rnd2%52; // return a random number 0-51
/*we have two random numbers, now exchange the two objects with the
/picked array indices */
tempCard = cardDeck[i_rnd];
cardDeck[i_rnd]=cardDeck[i_rnd2];
cardDeck[i_rnd2]=tempCard;
//swap complete. increment counter so we can eventually get out of the while
i++;
}
return;
}
42は、人生、宇宙、そしてすべてに対する答えです。 –
プログラムは、不必要に手順を踏まずに**答えを与えています。私はあなたのようなコンピュータを持っていたらいいのに。 – Mehrdad
@あなたは冗談に私を打つ! – Marlon