<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>www.steveneely.org &#187; algorithms</title>
	<atom:link href="http://www.steveneely.org/category/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.steveneely.org</link>
	<description>Steve Neely :: Meanderings in technology</description>
	<lastBuildDate>Wed, 20 Jan 2010 18:19:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Shuffles &amp; randoms</title>
		<link>http://www.steveneely.org/2010/01/20/shuffles-randoms/</link>
		<comments>http://www.steveneely.org/2010/01/20/shuffles-randoms/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 18:19:43 +0000</pubDate>
		<dc:creator>steve</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[shuffle]]></category>

		<guid isPermaLink="false">http://www.steveneely.org/?p=86</guid>
		<description><![CDATA[I was asked a question about shuffling yesterday which got me thinking. How do you write an algorithm to truly shuffle a deck of cards without any bias?
There are a couple of well-known algorithms to do this, both popularized by Donald Knuth. At a very abstract high-level they are:

generate a random number for each card [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked a question about shuffling yesterday which got me thinking. How do you write an algorithm to truly shuffle a deck of cards without any bias?</p>
<p>There are a couple of well-known algorithms to do this, both popularized by Donald Knuth. At a <em>very</em> abstract high-level they are:</p>
<ol>
<li>generate a random number for each card in the deck then sort the cards by number. If two cards are assigned the same number then try again;</li>
<li>go through the deck, taking each card in turn and swap it with some random position in the deck.</li>
</ol>
<p>Clearly #1 could take a longer time to run since you&#8217;ve got to sort cards and deal with clashes. Although with only 52 cards in a deck you are probably not too worried about algorithmic complexity.</p>
<p>#2 looks good on the surface but there are a few gotchas to be aware of. With a deeper mathematical analysis you can see why. The first is that if you swap cards with <em>any</em> position in the pack you will not get an even distribution with shuffles. This is because you&#8217;ve written an algorithm that has n^n execution paths and there are only n! permutations. Using the <a href="http://en.wikipedia.org/wiki/Shuffling#Poorly_implemented_Knuth_shuffles">wikipedia example</a> consider just 3 cards: your algorithm can produce 3^3 = 27 outcomes but there are only 6 permutations for shuffling. You cannot fit 27 into 6 so there must be some outcomes from your algorithm that are more likely (see pigeonhole principal).</p>
<p>The solution is to swap with the portion of the pack that has not yet been swapped with.</p>
<p>Wikipedia has a <a href="http://en.wikipedia.org/wiki/Shuffling#Poorly_implemented_Knuth_shuffles">clear article on shuffling and implementations</a> with further details on the impact of using the mod operator with random numbers (again, the space of randoms being generated then having mod applied is not an even distribution). A final note is that you need to seed your random number generator or it&#8217;ll be pseudo-random. Or better yet use <a href="http://www.random.org/">random.org</a></p>
<div class='bookmarkify'><a name='bookmarkify'></a><div class='title' title='Use these links to share this page with others'>Bookmark and Share</div><div class='linkbuttons'><a href='http://del.icio.us/post?url=http://www.steveneely.org/2010/01/20/shuffles-randoms/&amp;title=Shuffles &#038; randoms' title='Save to del.icio.us' onclick='target="_blank";' rel='nofollow'><img src='http://www.steveneely.org/wp-content/plugins/bookmarkify/delicious.png' style='width:16px; height:16px;' alt='[del.icio.us] ' /></a> <a href='http://digg.com/submit?phase=2&amp;url=http://www.steveneely.org/2010/01/20/shuffles-randoms/&amp;title=Shuffles &#038; randoms' title='Digg It!' onclick='target="_blank";' rel='nofollow'><img src='http://www.steveneely.org/wp-content/plugins/bookmarkify/digg.png' style='width:16px; height:16px;' alt='[Digg] ' /></a> <a href='http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.steveneely.org/2010/01/20/shuffles-randoms/&amp;title=Shuffles &#038; randoms' title='Save to Google Bookmarks' onclick='target="_blank";' rel='nofollow'><img src='http://www.steveneely.org/wp-content/plugins/bookmarkify/google.png' style='width:16px; height:16px;' alt='[Google] ' /></a> <a href='http://www.linkedin.com/shareArticle?mini=true&url=http://www.steveneely.org/2010/01/20/shuffles-randoms/&title=Shuffles &#038; randoms' title='Share on LinkedIn' onclick='target="_blank";' rel='nofollow'><img src='http://www.steveneely.org/wp-content/plugins/bookmarkify/linkedin.png' style='width:16px; height:16px;' alt='[LinkedIn] ' /></a> <a href='http://www.stumbleupon.com/submit?url=http://www.steveneely.org/2010/01/20/shuffles-randoms/&amp;title=Shuffles &#038; randoms' title='Stumble It!' onclick='target="_blank";' rel='nofollow'><img src='http://www.steveneely.org/wp-content/plugins/bookmarkify/stumbleupon.png' style='width:16px; height:16px;' alt='[StumbleUpon] ' /></a> <a href='https://favorites.live.com/quickadd.aspx?mkt=en-us&amp;url=http://www.steveneely.org/2010/01/20/shuffles-randoms/&amp;title=Shuffles &#038; randoms' title='Save to Windows Live' onclick='target="_blank";' rel='nofollow'><img src='http://www.steveneely.org/wp-content/plugins/bookmarkify/windowslive.png' style='width:16px; height:16px;' alt='[Windows Live] ' /></a> <a href='http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;u=http://www.steveneely.org/2010/01/20/shuffles-randoms/&amp;t=Shuffles &#038; randoms' title='Save to Yahoo! Bookmarks' onclick='target="_blank";' rel='nofollow'><img src='http://www.steveneely.org/wp-content/plugins/bookmarkify/yahoo.png' style='width:16px; height:16px;' alt='[Yahoo!] ' /></a> <a href='http://www.feedburner.com/fb/a/emailFlare?itemTitle=Shuffles &#038; randoms&amp;uri=http://www.steveneely.org/2010/01/20/shuffles-randoms/&amp;loc=en_US' title='Email this to a friend' onclick='target="_blank";' rel='nofollow'><img src='http://www.steveneely.org/wp-content/plugins/bookmarkify/email.png' style='width:16px; height:16px;' alt='[Email] ' /></a>  <a title='See more bookmark and sharing options...' href='http://www.steveneely.org/2010/01/20/shuffles-randoms/#bookmarkify' rel='nofollow'><small>More&nbsp;&raquo;</small></a></div><div class='brand'><small><a href='http://www.bookmarkify.com/'>Powered by Bookmarkify&trade;</a></small></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.steveneely.org/2010/01/20/shuffles-randoms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
