shuffling a deck of cards

Started by
7 comments, last by omegasyphon 22 years, 1 month ago
ok i have come up with this easy algorithm for shuffling a deck of cards, but it is horrendesly slow and not sure how to speed it up or what a better solution is. basically its like this: i have an array of 52 elements i seed the first element with a random variable of 2-52 and it then goes through the deck searching to see that the next random variable it creates isnt already in it. now this works fine but on a slow processor under 30mhz its dam slow.
Advertisement
An abstract demonstration of a better method:

  unsigned int Deck[52], a;/* Fill the array with one of each card type */for(a=0; a<52; ++a)  Deck[a] = a;/* Swap the cards X amount of times */srand(time(NULL));for(a=0; a<SOME_VALUE; ++a) {  unsigned int first, second, temp;  first = (unsigned)((rand()/(float)RAND_MAX)*52.0f);  second = (unsigned)((rand()/(float)RAND_MAX)*52.0f);  temp = Deck[first];  Deck[first] = Deck[second];  Deck[second] = temp;}  

I''m pretty sure you can see what I''m doing from that example.


A simple alternative idea (Null and Void''s method is probably better in terms of clarity and data visits):

int CompareFn( const void *elem1, const void *elem2 ){	return rand() - (RAND_MAX/2);}void Shuffle(){	srand(...);	qsort( Deck, 52, sizeof(CARD), compareFn );} 


the idea being to sort the cards in a random order - it could also be trivially modified to simulate different types of shuffle.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Here''s how I''ve always done it. Works just fine for me.

for (BYTE i=0; i<52; i++)
SwapCards(i, rand()%52);

If you''re worried about it not making things random enough, run it a hundred times. It''s so fast that your user won''t notice the difference

---
John Hattan
The Code Zone
Sweet software for a saturnine world

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

is swapcards a user defined function?
quote:Original post by S1CA

A simple alternative idea (Null and Void''s method is probably better in terms of clarity and data visits):

int CompareFn( const void *elem1, const void *elem2 ){	return rand() - (RAND_MAX/2);}void Shuffle(){	srand(...);	qsort( Deck, 52, sizeof(CARD), compareFn );}  


the idea being to sort the cards in a random order - it could also be trivially modified to simulate different types of shuffle.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com



hmmm... I think qsort has problems with compare not always returning the same values, it never finishes sorting and eventually causes a stack overflow. Haven''t tried that exact method but I''m pretty sure. Null and Void had the method of choice.
I think this is a different form of Michaelson''s post.

for ( int i = 0; i < 52; i++ )
{
int nRandomCard = (rand()%(52-i))+i;
SWAP( i, nRandomCard );
}

basically, you remove a random card from the original deck and add it to a new deck that you are creating at the top of the deck. The complicated line (rand()%(52-i))+i makes it so that you pick a random number in the half of the deck you haven''t shuffled yet.
Tsk, tsk, tsk, why reinvent the wheel ?

#include <algorithm>Card deck[52];std::random_shuffle( deck, deck+52 );  


Note: that version uses rand() internally. Make sure to call srand().

[edited by - Fruny on March 25, 2002 11:24:56 PM]
"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
DAMMIT! Fruny beat me to it.

This topic is closed to new replies.

Advertisement