The Fisher-Yates shuffle is an algorithm I came up with myself, independent of any previous work... So I'll give you my view on it, can I?

As and example take a deck of cards, 52 of them. Pick any card. You'll agree that the card chosen is completely random, no? No preference towards the old order? OK. Now the most important step: take it out of the deck. That's your first card of the new, shuffled deck.

Now you have a deck of 51 cards left. Pick one, any one... That's your second card, completely random. Now you have a deck of 50 cards left...

Repeat, taking one random card out of the deck each time, and adding it to the new deck, until you're left with an empty deck, with no cards left. Your new deck now contains all cards and is shuffled completely randomly.

As an optimisation for the implementation, you can see that the total number of cards in both decks is always the same. So instead of physically creating two decks, we take a paper bookmark and insert it between the old and the new deck. We move the chosen card out of the old deck and into the new deck, simply by swapping the chosen card and the former first card of the old deck, and then moving the bookmark one position up so that it's now the last card of the new deck.

Since the order of the new deck is completely independent of the order in the old deck, doing it this way, changing the order of the old deck while you're at it, doesn't influence the statistical properties of the end result, at all.


In reply to Re: Fisher-Yates theory by bart
in thread Fisher-Yates theory by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.