<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>Unruly Rambling - random tag</title>
  <link>http://www.shoesobjects.com/blog/tags/random/</link>
  <description>My thoughts on software, technology, and life in general</description>
  <language>en</language>
  <copyright>Mike Shoemaker</copyright>
  <lastBuildDate>Sun, 31 Jan 2010 17:47:37 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  
  <item>
    <title>How would you write a shuffle algorithm?</title>
    <link>http://www.shoesobjects.com/blog/2005/12/01/how_would_you_write_a_shuffle_algorithm.html</link>
    
      
        <description>
          &lt;p&gt; As I&#039;m beginning to write my card game application, I&#039;ve approached the unit test where I need to shuffle the deck.  I&#039;ve implemented it based on Knuth&#039;s Shuffle algorithm.  My question is, what are some clever ways that other programmers have implemented it?   &lt;/p&gt;
&lt;p&gt; My current implementation works like this.    &lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Put all 52 cards in an array&lt;/li&gt;
    &lt;li&gt;For Loop starting at element 51, working backwards&lt;/li&gt;
    &lt;li&gt;Generate random number between 0 and loop element index&lt;/li&gt;
    &lt;li&gt;Swap current element card with the element at the random index generated in previous step&lt;/li&gt;
    &lt;li&gt;Finish when loop index reaches the zero element.  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt; Delima with current implementation &lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Is there a bias with generating a random number between 0 and the loop index?  I&#039;m doing so with java.util.Random&lt;/li&gt;
    &lt;li&gt;Would multiple shuffle iterations over the deck solve this?&lt;/li&gt;
    &lt;li&gt;Is it worth refactoring the shuffle algorithm to use the strategy pattern so I can have multiple implementations of shuffle?  After all, this is a simple command line game, not Harrah&#039;s next killer online gambling app.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
        </description>
      
      
    
    
    
    <category>java</category>
    
    <comments>http://www.shoesobjects.com/blog/2005/12/01/how_would_you_write_a_shuffle_algorithm.html#comments</comments>
    <guid isPermaLink="true">http://www.shoesobjects.com/blog/2005/12/01/how_would_you_write_a_shuffle_algorithm.html</guid>
    <pubDate>Fri, 02 Dec 2005 05:59:32 GMT</pubDate>
  </item>
  
  </channel>
</rss>

