[A83] Re: shuffle code help please


[Prev][Next][Index][Thread]

[A83] Re: shuffle code help please




> 
> I read some stuff on this a while ago, and found that one of the most
> elegant methods of shuffling a deck of cards was this:
> You start with a variable equal to 52, and get a random number from it
> (0-51).  You take the card with this number, move the cards on top of it
> back one, move it to the top of the deck, decrement the variable to 51,
and
> do it over.  So the next time you get a random number from 0-50, take that
> card out, move the cards on top of it back one spot, move it to the top of
> the deck and repeat.  You quit when you decrement your variable and it is
> zero =)
> This algorithm is guaranteed to be random.
> - - Joe
> joe@joepnet.com
> 

A suggestion:
instead of moving up all cards in the deck, you could also swap the last
card
and the random card. (Say the random function returns 5, you'd swap card 5
and card 52, the next iteration random returns 8, you swap 8 and 51.)
(I didn't invent this myself, I once saw a program that did its shuffling
this
way.)

Rob van Wijk

-- 
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net




References: