<?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>shuffle | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/shuffle/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Thu, 22 Oct 2020 19:44:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>shuffle | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>More on Shuffledness</title>
		<link>https://learncgames.com/more-on-shuffledness/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=more-on-shuffledness</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Fri, 23 Oct 2020 23:00:05 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[cards]]></category>
		<category><![CDATA[deck]]></category>
		<category><![CDATA[shuffle]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=1661</guid>

					<description><![CDATA[<p>So I&#8217;ve updated my shuffledness calculation. This is a bit of a work in progress so probably isn&#8217;t the last version.  One of the things I&#8217;m interested in is what is the optimal number of swaps to get a good shuffled deck of cards. The shuffle algorithm picks two random indexes in the array 0-51 [&#8230;]</p>
The post <a href="https://learncgames.com/more-on-shuffledness/">More on Shuffledness</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:431,&quot;href&quot;:&quot;https:\/\/creazilla.com&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260206151339\/https:\/\/creazilla.com\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-17 15:19:31&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-21 23:07:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-06 23:28:36&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-11 09:11:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-25 09:36:44&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-03-25 09:36:44&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_1663" aria-describedby="caption-attachment-1663" style="width: 300px" class="wp-caption alignleft"><img fetchpriority="high" decoding="async" class="size-medium wp-image-1663" src="https://learncgames.com/wp-content/uploads/2020/10/cards-clipart-md-300x222.png" alt="DEck of playing cards" width="300" height="222" srcset="https://learncgames.com/wp-content/uploads/2020/10/cards-clipart-md-300x222.png 300w, https://learncgames.com/wp-content/uploads/2020/10/cards-clipart-md-768x567.png 768w, https://learncgames.com/wp-content/uploads/2020/10/cards-clipart-md.png 800w" sizes="(max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-1663" class="wp-caption-text"><a href="https://creazilla.com/">Dowload from Creazilla.com</a></figcaption></figure>
<p>So I&#8217;ve updated my shuffledness calculation. This is a bit of a work in progress so probably isn&#8217;t the last version.  One of the things I&#8217;m interested in is what is the optimal number of swaps to get a good shuffled deck of cards. The shuffle algorithm picks two random indexes in the array 0-51 and then swaps the contents.</p>
<p>Obviously doing this 10 times would only shuffle at most 20 cards and probably fewer as there&#8217;s no checks for repeats. The only check is that both indexes (indices?) are not the same. So I want to experiment and see what an optimal value for numTries should be..</p>
<pre><code class="language-c" data-line="">void ShuffleDeck(int numtries) {
	int firstIndex, secondIndex;
	for (int i = 0; i &lt; numtries; i++) {
		do {
			firstIndex = rand() % 52;
			secondIndex = rand() % 52;
		}
		while (firstIndex == secondIndex);
		int value = deck[firstIndex];
		deck[firstIndex] = deck[secondIndex];
		deck[secondIndex] = value;
	}
}</code></pre>
<p>Anyway, I&#8217;ve added a CalcDistance() function. This looks at each card and calculates how far it has moved from it&#8217;s original position.  It then divides this by the maximum distance it could have moved to get a move &#8220;factor&#8221;. These are summed up and divided by 52 to give an average move factor. That&#8217;s your CalcDistance() function.</p>
<pre><code class="language-c" data-line="">float CalcDistance() {
	float distance = 0.0f;
	for (int i = 0; i &lt; 52; i++) {
		float distanceDiv = (i &lt; 26) ? 51 - i : i-1;
		//printf(&quot;%i: %3.0f\n&quot;, i, distanceDiv);
		distance += (abs(deck[i] - i) / distanceDiv);
	}
	return distance/52.0f;
}</code></pre>
<p>The only thing odd about this function is the distanceDiv value which starts at 51, decreases to 25 then increases to 50. The printf let me check that was correct.</p>
<p>This is the current state of the program which I&#8217;ve listed fully below. Enjoy! It&#8217;s 63 lines long.</p>
<pre><code class="language-c" data-line="">// shuffledness.c : This measures how shuffled a deck of cards is
// cards are hjeld as values 0-51 in a 52 int aaray.

#include &lt;time.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int deck[52];
time_t t;

// Order the cards 0-51. 0 = Acew Hards,13 = Ace Clubs,26 = Ace of Diamonds and 51 = King of Spades
void InitDeck() {
	for (int i = 0; i &lt; 52; i++) {
		deck[i] = i;
	}
}

// Works on glovbal array deck and calculates a value for how displayed each card is
float CalcDisorder() {
	int total=0;
	for (int i = 1; i &lt; 52; i++) {
		total += abs(deck[i] - deck[i - 1]);
	}
	printf(&quot;Total = %d\n&quot;, total);
	return total / (52.0f * 26.0f);
}

// Sum up distance card moved/max distance
float CalcDistance() {
	float distance = 0.0f;
	for (int i = 0; i &lt; 52; i++) {
		float distanceDiv = (i &lt; 26) ? 51 - i : i-1;
		//printf(&quot;%i: %3.0f\n&quot;, i, distanceDiv);
		distance += (abs(deck[i] - i) / distanceDiv);
	}
	return distance/52.0f;
}

// Shuffle a deck by swapping two random cards a specified number of times
void ShuffleDeck(int numtries) {
	int firstIndex, secondIndex;
	for (int i = 0; i &lt; numtries; i++) {
		do {
			firstIndex = rand() % 52;
			secondIndex = rand() % 52;
		}
		while (firstIndex == secondIndex);
		int value = deck[firstIndex];
		deck[firstIndex] = deck[secondIndex];
		deck[secondIndex] = value;
	}
}

int main() {
	/* Intializes random number generator */
	srand((unsigned)time(&amp;t));
	for (int i = 0; i &lt; 25; i++) {
		InitDeck();
		ShuffleDeck(1000);
		printf(&quot;CalcDisorder() ==%f x %f = %f\n&quot;, CalcDisorder(),CalcDistance(),(double)CalcDisorder() * CalcDistance());
	}
	return 0;
}
</code></pre>
<p>Because I&#8217;m multiplying the two factors (disorder and Distance, the final value is never getting much above 0.35 and I&#8217;d prefer it to be in the range 0-0.999. Suggestions welcomed.</p>
<p>.</p>The post <a href="https://learncgames.com/more-on-shuffledness/">More on Shuffledness</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1661</post-id>	</item>
		<item>
		<title>How to measure how shuffled a deck of cards is</title>
		<link>https://learncgames.com/how-to-measure-how-shuffled-a-deck-of-cards-is/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-measure-how-shuffled-a-deck-of-cards-is</link>
					<comments>https://learncgames.com/how-to-measure-how-shuffled-a-deck-of-cards-is/#comments</comments>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Mon, 12 Oct 2020 23:00:54 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[card]]></category>
		<category><![CDATA[deck]]></category>
		<category><![CDATA[measure]]></category>
		<category><![CDATA[shuffle]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=1589</guid>

					<description><![CDATA[<p>I first thought about this when I wrote the program to shuffle  deck of cards using a riffle shuffle. If you are given a deck of cards (or pack of cards as us Brits say), how do you discern just how shuffled the pack is? Can you calculate a numeric value for it say a [&#8230;]</p>
The post <a href="https://learncgames.com/how-to-measure-how-shuffled-a-deck-of-cards-is/">How to measure how shuffled a deck of cards is</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:455,&quot;href&quot;:&quot;https:\/\/www.reddit.com\/r\/math\/comments\/aijngj\/can_you_quantify_how_shuffled_a_deck_of_cards_is&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20250330064847\/https:\/\/www.reddit.com\/r\/math\/comments\/aijngj\/can_you_quantify_how_shuffled_a_deck_of_cards_is\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 15:14:44&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-02-15 08:36:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-18 17:15:45&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-21 23:07:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-01 23:44:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-06 23:28:36&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-11 09:11:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-18 03:02:19&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-26 23:25:18&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-31 21:26:19&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-03-31 21:26:19&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'></div>
<p><img decoding="async" class="alignleft size-medium wp-image-1592" src="https://learncgames.com/wp-content/uploads/2020/10/openeddeck-1-300x281.jpg" alt="Opened deck plus new deck of cards" width="300" height="281" srcset="https://learncgames.com/wp-content/uploads/2020/10/openeddeck-1-300x281.jpg 300w, https://learncgames.com/wp-content/uploads/2020/10/openeddeck-1.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" />I first thought about this when I wrote the program to shuffle  deck of cards using a riffle shuffle. If you are given a deck of cards (or pack of cards as us Brits say), how do you discern just how shuffled the pack is? Can you calculate a numeric value for it say a % ranging from 0 to 100?</p>
<p>I believe it&#8217;s possible.  Here&#8217;s how.</p>
<ol>
<li>Start with a default pack of cards in perfect sorted order. Out of curiosity I found an unopened deck oif Waddington&#8217;s cards and opened it as the photos show. The cards in the pack were arranged in order King, Queen, Jack down to Ace in each of the four suits Heart, Clubs, Diamonds and Spades in that order. Let&#8217;s reverse the card rank ordering so a full deck starts with Ace Hearts through to King Hearts, Ace of Clubs to King of Clubs and so on with the last card being the King of Spades.</li>
<li>Instead of referring to cards by their rank and suite lets just number them 0-51. 0= Ace of Hearts, 51 = King of Spades.</li>
<li>When a deck is shuffled, each card can move to any other position. So a measure of shuffledness is calculating how far the cards moved in aggregate.</li>
<li>However the card movements have to be &#8220;normalized&#8221;. Cards 0 and 51 can move to any of 51 positions while cards 26 (King of Clubs) and 27 (Ace of Diamonds) can only move a maximum of 26 places.</li>
<li>I&#8217;m looking at the absolute value of a movement so if card 3 moves to position 47, it has moved 44 places and likewise card 47 moving to position 3 moves 44 (not -44) places.</li>
<li>So to normalize a card&#8217;s move, divide its move by its maximum possible distance it can move. So wherever card 0 moves divide it by 51, card 1 by 50, card 26 &#8216; move by 26.</li>
<li>Sum up all 52 normalized move&#8217;s and multiply by 100. That is the measure of shuffledness.</li>
</ol>
<p>I&#8217;ll write a C program to measure how shuffled a deck is and publish it in a day or two. Also here is <a title="Link to Reddit conversation on shuffledness" href="https://www.reddit.com/r/math/comments/aijngj/can_you_quantify_how_shuffled_a_deck_of_cards_is/" target="_blank" rel="nofollow noopener noreferrer">a conversation on Reddit</a> about shuffling cards.</p>
<p>&nbsp;</p>The post <a href="https://learncgames.com/how-to-measure-how-shuffled-a-deck-of-cards-is/">How to measure how shuffled a deck of cards is</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://learncgames.com/how-to-measure-how-shuffled-a-deck-of-cards-is/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1589</post-id>	</item>
	</channel>
</rss>
