I'm trying to shuffle the cards in a deck. I create two numbers randomly and assign their values to eachother 100 times. But it doesn't create randomly. Weirdest part is this, I put a breakpoint to line "counter++;" to observe if the program really chooses different cards each time. When I'm debugging, if I click on the continue button too quickly it creates same index1 and same index2 every time(but index1 is not equal to index2), otherwise different ( see the image ). I thought it may be due to the time. (when I click too quickly on the continue button or when I don't put breakpoint at all, it creates same numbers due to the same time.) But if that's the case, why does the program creates different values for index1 and index2? There is no breakpoint between their lines. So the time must be very small. It creates different indexes though. I'm confused. How to get rid of?
void shuffle_cards(string *a){
srand(time(NULL));
if (counter == 100){
return;
}
string temp;
int index1;
index1 = rand() % 52;
temp = a[index1];
int index2;
index2 = rand() % 52;
cout << a[index1] << " " << a[index2] << endl; //These lines are only for testing
a[index1] = a[index2];
a[index2] = temp;
cout << a[index1] << " " << a[index2] << endl; //These lines are only for testing
counter++;
srand(time(NULL));
shuffle_cards(a);
}
Aucun commentaire:
Enregistrer un commentaire