<?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>cards | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/cards/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>cards | 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>
	</channel>
</rss>
