VbDestroyer is correct - If you use the method stated, you will get duplicate cards (well... you have a > 99% chance getting at least one duplicate)
To do it better (one right way of doing it) :
First set up the cards in your array so that each card (if it is indexed 0 to 51) has it's ideal index value.
Then loop though each card. For each card select a "random" value from 0 to 51 ( rand() % 52; ) That's the second card's index. Swap the card's places. ie.
temp=card; card=card[j]; card[j]=card; </pre> <br>It's ok if i=j by chance.<br><br>You can loop though multiple times if you aren't satisfied by the results, but hey - at least now you won't have duplicates.<br><br>Good Luck,<br>-Michael<br><br>edit: stupid thing thinks the is italics<br>Btw, this is kinda like the oposite of what's known as the Selection Sort: Check it out. </i> <br><br><SPAN CLASS=editedby>[edited by - thr33d on September 2, 2003 7:50:40 PM]</SPAN>
quote:Original post by Thr33d Then loop though each card. For each card select a "random" value from 0 to 51 ( rand() % 52; ) That''s the second card''s index. Swap the card''s places. ie.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Thr33d Then loop though each card. For each card select a "random" value from 0 to 51 ( rand() % 52; ) That''s the second card''s index. Swap the card''s places.
Not completely random. You choose 52 random numbers from [0, 52), so there are 5252 equally likely sequences of numbers outputted by the RNG. However, there are only 52! permutations of a deck of cards. Since 5252 is not evenly divisible by 52!, some permutations must be more likely than others.
Instead, loop through each card (except the first). For each card i, swap it with a random card j where 0 <= j <= i. In other words: std::random_shuffle(card, card+52).
Yeah, you're right (so far as I can tell) I'm not so great with proofs of stuff in math, I kinda work from my mind and the practical side of things.
But what you say makes sense: In the way I mentioned, you repick many of the first cards to be switched with the later cards. I don't really see how this could somehow create any less random distrubution of sets of cards, but, ah, I can see that my concept was off base.
You're right also with the 52^52 and the 52! math parts I now see.
When going through a deck of cards, the first card is (theoretically) completely random. As you choose the next card, it is a random card of the remaining. (My logic is correct, right?) When you get to the last card you need not swap it, it has most likely been swapped, and if not, well, it's just part of the chance of things (required for even spread of cards.)
the swap function would be defined somewhere else (just swaps two variable's values)
As I saw it though, in what you wrote " Instead, loop through each card (except the first). For each card i, swap it with a random card j where 0 <= j <= i. " You seem to be taking a backwards approach to it, but it doesn't quite look right. (Because you're swapping swapped cards, which seems really funky to me - kinda like what I was doing before) " In other words: std::random_shuffle(card, card+52). " Can't comment on this - I don't know how random_shuffle works I'm more of a Java man myself.
Please tell me if my math is off, and explain to me if I'm wrong in my interpretation of your listed method of shuffling. Also, please let me know how random_shuffle works if you reply to other stuff of mine.
Thanks, -Michael
[edited by - thr33d on September 3, 2003 11:45:24 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan