Удивительная случайность, как эта, делает паршивый способ «перемешивать» массив «карт».Тот факт, что я получаю одно и то же число, говорит о том, что у меня возникли проблемы с сбором отдельных семян каждый раз.Я неправильно использую srand48
или time(NULL)
?Есть ли какая-то логическая ошибка, по которой я скучаю?Разве не хватает времени между итерациями, чтобы значение time()
отличалось?
Код выполняется в 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;
}