Re: Shuffling a linked list




"A. Farber" <Alexander.Farber@xxxxxxxxx> wrote in message
news:1167407792.061885.180820@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello,

I have a doubly-linked list of 32 playing cards, which I'd like to
randomly and evenly (!) shuffle.

(For several reasons I'm not using an array, which I could go
through once, keeping swapping cards at random positions.)

I have a list head with 2 pointers - to the very 1st and to the
last elements and I have 2 macros to insert an element at the
"head" or at the "tail".

Does anybody please have a good algorithm for me?

You have 2 lists: an input list and an output list. At the start of the
algorithm, your input list is of length 32, and your output list is of
length 0. At the end of the algorithm, your input list will be length 0 and
your output list will be length 32.

while(there are still cards in the input list) {
choose a card randomly from the input list.
Move that card to the tail of the output list.
}

- Oliver


.



Relevant Pages

  • Re: Observer pattern limitations
    ... The components are placed in an array of lists. ... So to run the algorithm, you iterate through the ...
    (comp.object)
  • Re: Observer pattern limitations
    ... In the data-flow language I designed, ... The components are placed in an array of lists. ... So to run the algorithm, ...
    (comp.object)
  • Re: Opinions, please, on vocabulary flashcard organization
    ... The cards are much simpler. ... flashcards. ... criteria simultaneously, e.g. success rate, travel vocabulary, nouns ... - add functions to import/export/merge the flashcard lists. ...
    (alt.usage.english)
  • Re: How many bytes per Italian character?
    ... it is a hyrbrid solution between array and list ... Another cool algorithm I did some years ago was to create sparse arrays with constant time ... was to represent parser state tables, where the dimensionality is n*m, for n the number of ... for large lists without thinking too hard about it. ...
    (microsoft.public.vc.mfc)
  • Re: Create-A-Clan Spoof Cards
    ... > of interesting cards to spoof (not necessarily just for combat decks) ... > while wandering through the lists: ...
    (rec.games.trading-cards.jyhad)