<?xml-stylesheet type="text/css" href="http://www.mikepope.com/blog/rss/rssComments.css" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" >
  <channel>
    <title>Shuffle - Comments</title>
    <link>http://www.mikepope.com/blog/AddComment.aspx?blogid=1851</link>
    <description />
    <copyright>Copyright 2003-2007 Mike Pope </copyright>
    <dc:language>en-US</dc:language>
    <pubDate>Sat, 01 Dec 2007 23:59:03 GMT</pubDate>
    <generator>mike pope's Web log</generator>
    <webMaster>mike@mikepope.com</webMaster>
        <item>
      <title>Comment by Rik Hemsley on "Shuffle"</title>
      <link>http://rikkus.info</link>
      <guid>http://www.mikepope.com/blog/AddComment.aspx?blogid=1851#1851_1858</guid>
      <description>I found a well designed algorithm here: &lt;a target='_blank' href='http://www.secureprogramming.com/?action=view&amp;amp;feature=recipes&amp;amp;recipeid=23'&gt;http://www.secureprogramming.com/?action=view&amp;amp;feature=recipes&amp;amp;recipeid=23&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;But for my toy card game, written in Ruby, I ended up using something very simple, but effective:&lt;br /&gt;&lt;br /&gt;cards.sort_by { rand }&lt;br /&gt;</description>
      <dc:creator>Rik Hemsley</dc:creator>
      <pubDate>Sun, 02 Dec 2007 01:46:51 GMT</pubDate>
    </item>
    <item>
      <title>Comment by Julian on "Shuffle"</title>
      <link>http://www.somethinkodd.com/oddthinking</link>
      <guid>http://www.mikepope.com/blog/AddComment.aspx?blogid=1851#1851_1859</guid>
      <description>I use an algorithm that I learnt by studying a long-forgotten Commodore-64 BASIC program in the mid-80s.&lt;br /&gt;&lt;br /&gt;create an array of 52 cards.&lt;br /&gt;for X in 1..52&lt;br /&gt;{&lt;br /&gt;   R = random number from 1..52&lt;br /&gt;   Swap card X with card R.&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Advantages:&lt;br /&gt; * Simple.&lt;br /&gt; * Takes a fixed, short amount of time to run. &lt;br /&gt; * Doesn't require two arrays; it's all done in place.&lt;br /&gt;&lt;br /&gt;</description>
      <dc:creator>Julian</dc:creator>
      <pubDate>Sun, 02 Dec 2007 02:19:51 GMT</pubDate>
    </item>
    <item>
      <title>Comment by mike on "Shuffle"</title>
      <link>http://www.mikepope.com/blog/</link>
      <guid>http://www.mikepope.com/blog/AddComment.aspx?blogid=1851#1851_1860</guid>
      <description>Julian, I added the Array Swap algorithm. Seems about in the same range, time wise, tho it is, as you note, simpler and presumably takes less memory.&lt;br /&gt;&lt;br /&gt;One problem, of course, is that it's difficult to assign a degree of randomness to any one of the approaches. For me, anyway ...</description>
      <dc:creator>mike</dc:creator>
      <pubDate>Sun, 02 Dec 2007 08:34:15 GMT</pubDate>
    </item>
    <item>
      <title>Comment by Eric Lippert on "Shuffle"</title>
      <link>http://blogs.msdn.com/ericlippert</link>
      <guid>http://www.mikepope.com/blog/AddComment.aspx?blogid=1851#1851_1865</guid>
      <description>Your original algorithm -- for each source card pick a destination slot at random, try again if it is already filled -- is O(n^2) on average for a deck with n cards. (Proving this is straightforward, you just have to solve a very simple probabalistic recurrance, email me if you want the details.)  But yes, it is gross and the running time is probabalistic -- it could be arbitrarily long if you have bad luck.&lt;br /&gt;&lt;br /&gt;Your next attempt -- for each destination slot, pick a random source slot, compact the deck every time -- is also O(n^2), but at least is bounded. There cannot be more than (n/2)(n+1) array moves during compaction.&lt;br /&gt;&lt;br /&gt;Your third attempt -- for each slot, swap randomly with another member -- is efficient at O(n) but has the unfortunate property that some final configurations are more common than others, ie, this algorithm has bias. (Proof: do it with a deck of three cards. There are six possible shufflings. The algorithm does three swaps on three cards so there are 3^3 = 27 possible sequences produced.  6 does not go evenly into 27, so some of the 6 possbilities must be overrepresented in the 27 possible outcomes.)&lt;br /&gt;&lt;br /&gt;The standard way of implementing this algorithm is: associate each card with a random real number between 0.0 and 1.0.  Sort the list based on its associated number. That's O(n log n) and has no bias.&lt;br /&gt;&lt;br /&gt;Note that the randomness source should be crypto strength.  Otherwise it is possible to deduce the state of the entire deck from a portion of it.  See my commentary on Greg's article here:&lt;br /&gt;&lt;br /&gt;&lt;a target='_blank' href='http://blogs.msdn.com/gstemp/archive/2004/02/23/78434.aspx'&gt;http://blogs.msdn.com/gstemp/archive/2004/02/23/78434.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;(Just substitute &quot;cards in a hand&quot; for &quot;characters in a password&quot;.)&lt;br /&gt; </description>
      <dc:creator>Eric Lippert</dc:creator>
      <pubDate>Mon, 03 Dec 2007 11:26:00 GMT</pubDate>
    </item>
    <item>
      <title>Comment by Julian on "Shuffle"</title>
      <link>http://www.somethinkodd.com/oddthinking</link>
      <guid>http://www.mikepope.com/blog/AddComment.aspx?blogid=1851#1851_1876</guid>
      <description>Jeff Atwood took up this challenge in two posts.&lt;br /&gt;&lt;br /&gt;&lt;a target='_blank' href='http://www.codinghorror.com/blog/archives/001008.html'&gt;http://www.codinghorror.com/blog/archives/001008.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a target='_blank' href='http://www.codinghorror.com/blog/archives/001015.html'&gt;http://www.codinghorror.com/blog/archives/001015.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I was forced to admit that he was right and I was wrong.&lt;br /&gt;&lt;br /&gt;&lt;a target='_blank' href='http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/'&gt;http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Sorry.&lt;br /&gt;</description>
      <dc:creator>Julian</dc:creator>
      <pubDate>Sat, 08 Dec 2007 23:46:55 GMT</pubDate>
    </item>
    <item>
      <title>Comment by mike on "Shuffle"</title>
      <link>http://www.mikepope.com/blog/</link>
      <guid>http://www.mikepope.com/blog/AddComment.aspx?blogid=1851#1851_1877</guid>
      <description>Yeah, I followed up:&lt;br /&gt;&lt;br /&gt;&lt;a target='_blank' href='http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1858'&gt;http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1858&lt;/a&gt;</description>
      <dc:creator>mike</dc:creator>
      <pubDate>Sun, 09 Dec 2007 00:44:11 GMT</pubDate>
    </item>

  </channel>
</rss>

