Article View: rec.games.programmer
Article #1149Re: Card Shuffling
From: Huw Roberts
Date: Wed, 11 Nov 1992 17:00
Date: Wed, 11 Nov 1992 17:00
53 lines
2269 bytes
2269 bytes
u519876@csi.UOttawa.CA (U. N. derGrad) writes:
: Hi there,
:
: I'm looking for a quick and simple algorithm for shuffling a normal
: deck of cards. A joker or two could also be added to the deck (I'm
: making a poker game).
:
: Any suggestions? Programming language is C, on a Unix station.
:
: Much appreciated,
:
HeroSoftware@cup.portal.com (Bits of Magic) writes
:
: A quick and dirty way is:
:
: [quick and dirty way deleted]
:
: Basically it's just:
: swap two randomly selected cards in the deck
: repeat as needed
This is THE WORST way to randomise a deck of cards. Your value for SWAPS
has to be HUGE (over 1000) in order to get any degree of randomness.
This algorithm does not guarantee that every card is swapped at least once.
ttobler@unislc.uucp (Trent Tobler) writes
: make a single pass through the array of cards, and swap each card with another
: card chosen at random: ie:
:
: for i = 1 to NUM_CARDS
: r = random number between 1 and NUM_CARDS
: swap card[i] and card[r]
: end for
:
: As long as the random generator is reasonably random, this will shuffle the
: deck.
This will shuffle the deck but will not give a decent random distribution.
This was discussed recently on one of the bridge groups (or perhaps it was
here. I don't remember). It is certainly sufficiently unrandom to seriously
affect the hands you get in a bridge game.
Try this one (guarantees that each possible ordering is equally likely given
a perfectly random random number generator).
for (i = NUM_CARDS; i > 0; i--)
r = random number between 1 and i inclusive
swap card[i] and card[r] (may be the same card)
Huw
--
Huw Roberts (huw@siesoft.co.uk) Siemens Nixdorf Information Systems Ltd.
+44 344 850982 Oldbury, Bracknell, Berks. RG12 4FZ England
Technical Details
Message-ID:
<1992Nov11.170046.11528@siesoft.co.uk>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!siesoft!huwReferences:
<1992Nov6.163008.19400@csi.uottawa.ca>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!uknet!siesoft!huw From: huw@siesoft.co.uk (Huw Roberts) Newsgroups: rec.games.programmer Subject: Re: Card Shuffling Message-ID: <1992Nov11.170046.11528@siesoft.co.uk> Date: 11 Nov 92 17:00:46 GMT References: <1992Nov6.163008.19400@csi.uottawa.ca> Sender: news@siesoft.co.uk (Usenet News) Organization: Siemens Nixdorf Information Systems Ltd. Lines: 53 X-Newsreader: Tin 1.1 PL4