<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.7" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: The Shuffle</title>
	<link>http://www.technicalinterviews.net/the-shuffle/</link>
	<description>This site features a collection of common technical interview questions gathered by a group of programmers who have been through, and given, lots of technical interviews. There is an emphasis on C++ and game programming technical interviews, but most of the questions are relevant to any technical interview.</description>
	<pubDate>Thu, 04 Dec 2008 19:15:33 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.7</generator>

	<item>
		<title>by: Sundar</title>
		<link>http://www.technicalinterviews.net/the-shuffle/#comment-1254</link>
		<pubDate>Thu, 12 Jul 2007 12:52:26 +0000</pubDate>
		<guid>http://www.technicalinterviews.net/the-shuffle/#comment-1254</guid>
					<description>Let X1, X2…. XN (In this case N=52) be the set of N numbers to be shuffled.

   1. Set j to N
   2. Generate a random number R. (uniformly distributed between 0 and 1)
   3. Set k to (jR+1). k is now a random integer, between 1 and j.
   4. Exchange Xk and Xj
   5. Decrease j by 1.
   6. If j &#62; 1, return to step 2.

void KnuthShuffle(int* pArr)
{
    int rand;
    for(int i=51;i&#62;=0;i--)
    {
        rand=GenRand(0,i);
        swap(pArr[i], pArr[rand]);
    }    
}

GenRand(int min, int max) generates a random number between min and max.</description>
		<content:encoded><![CDATA[<p>Let X1, X2…. XN (In this case N=52) be the set of N numbers to be shuffled.</p>
<p>   1. Set j to N<br />
   2. Generate a random number R. (uniformly distributed between 0 and 1)<br />
   3. Set k to (jR+1). k is now a random integer, between 1 and j.<br />
   4. Exchange Xk and Xj<br />
   5. Decrease j by 1.<br />
   6. If j &gt; 1, return to step 2.</p>
<p>void KnuthShuffle(int* pArr)<br />
{<br />
    int rand;<br />
    for(int i=51;i&gt;=0;i&#8211;)<br />
    {<br />
        rand=GenRand(0,i);<br />
        swap(pArr[i], pArr[rand]);<br />
    }<br />
}</p>
<p>GenRand(int min, int max) generates a random number between min and max.
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
