• Popular Now

• 15
• 15
• 11
• 9
• 10

Archived

This topic is now archived and is closed to further replies.

shuffling a deck of cards

This topic is 5842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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.

Share on other sites

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

Share on other sites
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

Share on other sites
is swapcards a user defined function?

Share on other sites
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.

Share on other sites
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.

Share on other sites
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]