<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Shuffling and Ownage</title>
	<atom:link href="http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/</link>
	<description>A blog for odd things and odd thoughts.</description>
	<lastBuildDate>Wed, 01 Feb 2012 22:21:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Andrew Certain</title>
		<link>http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/comment-page-1/#comment-118374</link>
		<dc:creator>Andrew Certain</dc:creator>
		<pubDate>Mon, 12 May 2008 20:47:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/#comment-118374</guid>
		<description>&lt;blockquote cite=&quot;&quot;&gt;This may introduce a subtle bias in the gcc version if the number of items to be shuffled approaches RANDOM_MAX.&lt;/blockquote&gt;

This limitation is not correct, and the reality is much worse than your statement.  The problem happens if either the state space of the RNG is smaller than the possible states from which you want to choose (or the state space of the initializer is smaller than the output state space).  If you want to shuffle 52 cards, you have 52! output states, so you need at least 226 bits of state in the RNG (and it has to be initialized somehow with at least that many bits).
&lt;p&gt;As cited above, see the Wikipedia entry for &lt;a href=&quot;http://en.wikipedia.org/wiki/Fisher-Yates_shuffle&quot; rel=&quot;nofollow&quot;&gt;Fisher-Yates shuffle&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;
Andrew&lt;/p&gt;</description>
		<content:encoded><![CDATA[<blockquote cite=""><p>This may introduce a subtle bias in the gcc version if the number of items to be shuffled approaches RANDOM_MAX.</p></blockquote>
<p>This limitation is not correct, and the reality is much worse than your statement.  The problem happens if either the state space of the RNG is smaller than the possible states from which you want to choose (or the state space of the initializer is smaller than the output state space).  If you want to shuffle 52 cards, you have 52! output states, so you need at least 226 bits of state in the RNG (and it has to be initialized somehow with at least that many bits).</p>
<p>As cited above, see the Wikipedia entry for <a href="http://en.wikipedia.org/wiki/Fisher-Yates_shuffle" rel="nofollow" class="wikipedia">Fisher-Yates shuffle</a>.</p>
<p>
Andrew</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sunny Kalsi</title>
		<link>http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/comment-page-1/#comment-80394</link>
		<dc:creator>Sunny Kalsi</dc:creator>
		<pubDate>Tue, 11 Dec 2007 09:07:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/#comment-80394</guid>
		<description>I would submit that Jeff Atwood was told by someone else about his error, and in fact was owned himself. Jeff blogged on it, but didn&#039;t actually own anyone. There&#039;s some code ninja who has actually owned you by proxy...

Likely &lt;a href=&quot;http://www.xkcd.com/345/&quot; rel=&quot;nofollow&quot;&gt;this person&lt;/a&gt;.

I actually agreed when he said when it was complicated, because &lt;em&gt;I couldn&#039;t imagine myself doing it&lt;/em&gt;. I mean physically, with a set of cards. If I was being really anal about shuffling cards, I&#039;d do the following. In Java:
&lt;code&gt;
public static List shuffle(List cards)
{
    final List out = new LinkedList();
    for (final Card c : cards)
    {
        int index = (int) (Math.random() * (out.size() + 1));
        out.add(index, c);
    }
    return out;
}
&lt;/code&gt;

The test I wrote says it&#039;s not &quot;naive&quot;. Now all you need to do is choose a nice list which does O(1) inserts + O(1) lookups (clearly, an exercise for the reader).

PS: Damn I can&#039;t do code listing...</description>
		<content:encoded><![CDATA[<p>I would submit that Jeff Atwood was told by someone else about his error, and in fact was owned himself. Jeff blogged on it, but didn&#8217;t actually own anyone. There&#8217;s some code ninja who has actually owned you by proxy&#8230;</p>
<p>Likely <a href="http://www.xkcd.com/345/" rel="nofollow" class="liexternal">this person</a>.</p>
<p>I actually agreed when he said when it was complicated, because <em>I couldn&#8217;t imagine myself doing it</em>. I mean physically, with a set of cards. If I was being really anal about shuffling cards, I&#8217;d do the following. In Java:<br />
<code><br />
public static List shuffle(List cards)<br />
{<br />
    final List out = new LinkedList();<br />
    for (final Card c : cards)<br />
    {<br />
        int index = (int) (Math.random() * (out.size() + 1));<br />
        out.add(index, c);<br />
    }<br />
    return out;<br />
}<br />
</code></p>
<p>The test I wrote says it&#8217;s not &#8220;naive&#8221;. Now all you need to do is choose a nice list which does O(1) inserts + O(1) lookups (clearly, an exercise for the reader).</p>
<p>PS: Damn I can&#8217;t do code listing&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alastair</title>
		<link>http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/comment-page-1/#comment-80188</link>
		<dc:creator>Alastair</dc:creator>
		<pubDate>Mon, 10 Dec 2007 08:56:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/#comment-80188</guid>
		<description>&lt;blockquote&gt;That answer would be correct, except that you started with the premise of C++… :-)&lt;/blockquote&gt;

Guilty as charged...

Actually whilst poking around with this I found that the Visual C++ implementation of the &lt;code&gt;std::random_shuffle&lt;/code&gt; algorithm correctly handles the fact that &lt;code&gt;rand()&lt;/code&gt; has only a certain degree of precision, whereas the gcc implementation does &lt;em&gt;not&lt;/em&gt;. This may introduce a subtle bias in the gcc version if the number of items to be shuffled approaches &lt;code&gt;RANDOM_MAX&lt;/code&gt;.</description>
		<content:encoded><![CDATA[<blockquote><p>That answer would be correct, except that you started with the premise of C++… <img src='http://www.somethinkodd.com/oddthinking/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p></blockquote>
<p>Guilty as charged&#8230;</p>
<p>Actually whilst poking around with this I found that the Visual C++ implementation of the <code>std::random_shuffle</code> algorithm correctly handles the fact that <code>rand()</code> has only a certain degree of precision, whereas the gcc implementation does <em>not</em>. This may introduce a subtle bias in the gcc version if the number of items to be shuffled approaches <code>RANDOM_MAX</code>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aristotle Pagaltzis</title>
		<link>http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/comment-page-1/#comment-80180</link>
		<dc:creator>Aristotle Pagaltzis</dc:creator>
		<pubDate>Mon, 10 Dec 2007 07:34:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/#comment-80180</guid>
		<description>&lt;blockquote&gt;The correct answer of course is &lt;code&gt;std::random_shuffle&lt;/code&gt;.&lt;/blockquote&gt;

Heh. That answer would be correct, except that you started with the premise of C++… :&lt;b&gt;&lt;/b&gt;-)

The standard libraries of most languages probably have an equivalent. The Perl FAQ entry starts out “if your Perl isn’t ancient, use &lt;code&gt;List::Util::shuffle&lt;/code&gt;.” Atwood is a .NET guy; that huge framework more likely than not also has a shuffling function somewhere. Of course programmers are prone to reinvent seemingly-trivial functionality before they think of thumbing through their tool’s docs.</description>
		<content:encoded><![CDATA[<blockquote><p>The correct answer of course is <code>std::random_shuffle</code>.</p></blockquote>
<p>Heh. That answer would be correct, except that you started with the premise of C++… :<b></b>-)</p>
<p>The standard libraries of most languages probably have an equivalent. The Perl FAQ entry starts out “if your Perl isn’t ancient, use <code>List::Util::shuffle</code>.” Atwood is a .NET guy; that huge framework more likely than not also has a shuffling function somewhere. Of course programmers are prone to reinvent seemingly-trivial functionality before they think of thumbing through their tool’s docs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alastair</title>
		<link>http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/comment-page-1/#comment-80150</link>
		<dc:creator>Alastair</dc:creator>
		<pubDate>Mon, 10 Dec 2007 01:14:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/#comment-80150</guid>
		<description>This would be a good interview question.

The correct answer of course is &lt;code&gt;std::random_shuffle&lt;/code&gt;.

I had a brief play with it just now. Here it is shuffling a three card deck 600000 times:

&lt;code&gt;abc: 99973
acb: 100118
bac: 99733
bca: 99955
cab: 100043
cba: 100178
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>This would be a good interview question.</p>
<p>The correct answer of course is <code>std::random_shuffle</code>.</p>
<p>I had a brief play with it just now. Here it is shuffling a three card deck 600000 times:</p>
<p><code>abc: 99973<br />
acb: 100118<br />
bac: 99733<br />
bca: 99955<br />
cab: 100043<br />
cba: 100178<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aristotle Pagaltzis</title>
		<link>http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/comment-page-1/#comment-80111</link>
		<dc:creator>Aristotle Pagaltzis</dc:creator>
		<pubDate>Sun, 09 Dec 2007 15:56:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/#comment-80111</guid>
		<description>I could’ve told you, had I known you were involved in the first place.

I read a bunch about the derivation of the Fisher-Yates shuffle a few years back, because it’s given as &lt;a href=&quot;http://perldoc.perl.org/perlfaq4.html#How-do-I-shuffle-an-array-randomly%3f&quot; rel=&quot;nofollow&quot;&gt;the answer to “how do I shuffle an array?”&lt;/a&gt; in &lt;a href=&quot;http://perldoc.perl.org/perlfaq.html&quot; rel=&quot;nofollow&quot;&gt;the Perl documentation’s FAQs&lt;/a&gt;, and bias in shuffles came up in a number of threads on &lt;a href=&quot;http://www.perlmonks.org/&quot; rel=&quot;nofollow&quot;&gt;Perl Monks&lt;/a&gt;. One of them, that I remember well, discussed doing a quicksort where the comparison function declares the numbers equal or unequal half the time, respectively, based on the output of a random number generator. (&lt;code&gt;@shuffled&#160;=&#160;sort&#160;{&#160;rand&#160;&lt;&#160;.5&#160;}&#160;@orig&lt;/code&gt;) – which seems to work, but suffers from bias. It took me a while to understand why. That was my first contact with the subject matter.

So when I read Jeff’s article, I looked at that algorithm, scratched my head, went “err hmm… that looks biased”, and then put it out of my mind and went about my ways. I was not at all surprised to read Jeff’s tail-between-legs follow-up about that algorithm being broken and how it took him a while to understand why.

Anything that needs to be analysed stochastically is notoriously insidious to get right – particularly if it involves randomness. And of course we all know that “Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin.” —&lt;i&gt;John von Neumann&lt;/i&gt;</description>
		<content:encoded><![CDATA[<p>I could’ve told you, had I known you were involved in the first place.</p>
<p>I read a bunch about the derivation of the Fisher-Yates shuffle a few years back, because it’s given as <a href="http://perldoc.perl.org/perlfaq4.html#How-do-I-shuffle-an-array-randomly%3f" rel="nofollow" class="liexternal">the answer to “how do I shuffle an array?”</a> in <a href="http://perldoc.perl.org/perlfaq.html" rel="nofollow" class="liexternal">the Perl documentation’s FAQs</a>, and bias in shuffles came up in a number of threads on <a href="http://www.perlmonks.org/" rel="nofollow" class="liexternal">Perl Monks</a>. One of them, that I remember well, discussed doing a quicksort where the comparison function declares the numbers equal or unequal half the time, respectively, based on the output of a random number generator. (<code>@shuffled&nbsp;=&nbsp;sort&nbsp;{&nbsp;rand&nbsp;&lt;&nbsp;.5&nbsp;}&nbsp;@orig</code>) – which seems to work, but suffers from bias. It took me a while to understand why. That was my first contact with the subject matter.</p>
<p>So when I read Jeff’s article, I looked at that algorithm, scratched my head, went “err hmm… that looks biased”, and then put it out of my mind and went about my ways. I was not at all surprised to read Jeff’s tail-between-legs follow-up about that algorithm being broken and how it took him a while to understand why.</p>
<p>Anything that needs to be analysed stochastically is notoriously insidious to get right – particularly if it involves randomness. And of course we all know that “Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin.” —<i>John von Neumann</i></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alan Green</title>
		<link>http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/comment-page-1/#comment-80087</link>
		<dc:creator>Alan Green</dc:creator>
		<pubDate>Sun, 09 Dec 2007 11:57:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.somethinkodd.com/oddthinking/2007/12/09/shuffling-and-ownage/#comment-80087</guid>
		<description>Back in the days of Windows &#039;95, I wrote a program that shuffled 4 million, uniquely-numbered cards. It produced a file containing the card number, a prize description (most of which were, &quot;You can enter the 2nd chance draw!&quot;) and a verification code. The file was sent to a printing firm, which printed up the 4 million cards.

I got the shuffling algorithm badly wrong twice before finally devising a variant of Knuth&#039;s algorithm. At the time I was proud that it ran in O(number of cards) time and O(number of types of card) space while giving a pragmatically - if not theoretically - random distribution. Looking back, I&#039;m just relieved that there wasn&#039;t a lotteries board asking to see my random number generator :)</description>
		<content:encoded><![CDATA[<p>Back in the days of Windows &#8217;95, I wrote a program that shuffled 4 million, uniquely-numbered cards. It produced a file containing the card number, a prize description (most of which were, &#8220;You can enter the 2nd chance draw!&#8221;) and a verification code. The file was sent to a printing firm, which printed up the 4 million cards.</p>
<p>I got the shuffling algorithm badly wrong twice before finally devising a variant of Knuth&#8217;s algorithm. At the time I was proud that it ran in O(number of cards) time and O(number of types of card) space while giving a pragmatically &#8211; if not theoretically &#8211; random distribution. Looking back, I&#8217;m just relieved that there wasn&#8217;t a lotteries board asking to see my random number generator <img src='http://www.somethinkodd.com/oddthinking/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>

