<?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>riffle | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/riffle/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>Sat, 29 Aug 2020 16:17:33 +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>riffle | 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>How to calculate how effective a Riffle is</title>
		<link>https://learncgames.com/how-to-calculate-how-effective-a-riffle-is/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-calculate-how-effective-a-riffle-is</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Wed, 02 Sep 2020 23:00:14 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Recreational]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[riffle]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=1325</guid>

					<description><![CDATA[<p>In my previous post I mentioned about writing a program to determine how effective a riffle shuffle was. So here&#8217;s the code. How it works I&#8217;m using an array of 52 chars to hold the deck. I&#8217;m only interested in the card&#8217;s position in the deck so each card is initialised with a value in [&#8230;]</p>
The post <a href="https://learncgames.com/how-to-calculate-how-effective-a-riffle-is/">How to calculate how effective a Riffle 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;:535,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/422737-422737\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=627167&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:536,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=627167&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260205102120\/https:\/\/pixabay.com\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 15:57:27&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-15 11:44:29&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-21 02:36:50&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-27 11:04:46&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-09 21:15:19&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-14 12:23:09&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-21 05:41:45&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-30 20:40:58&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-05 09:29:47&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-09 05:33:13&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-12 07:31:21&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-16 15:32:43&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-20 09:40:36&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-24 01:53:03&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-24 01:53:03&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_1329" aria-describedby="caption-attachment-1329" style="width: 300px" class="wp-caption alignleft"><img fetchpriority="high" decoding="async" class="size-medium wp-image-1329" src="https://learncgames.com/wp-content/uploads/2020/09/cards-627167_640-300x225.jpg" alt="deck of cards" width="300" height="225" srcset="https://learncgames.com/wp-content/uploads/2020/09/cards-627167_640-300x225.jpg 300w, https://learncgames.com/wp-content/uploads/2020/09/cards-627167_640.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" /><figcaption id="caption-attachment-1329" class="wp-caption-text">Image by <a href="https://pixabay.com/users/422737-422737/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=627167">Hebi B.</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=627167">Pixabay</a></figcaption></figure>
<p>In my previous post I mentioned about writing a program to determine how effective a riffle shuffle was. So here&#8217;s the code.</p>
<h3>How it works</h3>
<p>I&#8217;m using an array of 52 chars to hold the deck. I&#8217;m only interested in the card&#8217;s position in the deck so each card is initialised with a value in the range 0-51. I&#8217;m using three other similar sized arrays.</p>
<ul>
<li><strong>tempCards</strong> are used purely for doing the riffle.</li>
<li><strong>distances</strong> are used to calculate the maximum distance the card moved</li>
<li><strong>startPos</strong> tracks the cards starting position 0-51 before doing lots of rounds of riffles.</li>
</ul>
<p>The program starts by picking up ( as a parameter) how many rounds you want it to run. It defaults to 10 if no value is input.</p>
<p>It then clears distances and inits cards. In each round, it starts by storing card positions in startPos. It then does seven riffles and works out how far a card has moved. If it has moved more than before (in distances) it stores it in distances.</p>
<p>DoRiffle works by indexing through the two 1/2 deck piles taking a card from each and a 50:50 random chance determines which of the two cards goes into the shuffled deck first and then second.</p>
<p>Here&#8217;s the listing.</p>
<pre><code class="language-c" data-line="">// riffle.c by D. Bolton (C) 2020 Learncgames.com - TYou are free to redistribute but please keep this line in

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

// #defines
#define MAXROUNDS 10
#define NUMRIFFLES 7
#define NUMCARDS 52

// variables
int NumRounds;
char cards[NUMCARDS],tempCards[NUMCARDS],startPos[NUMCARDS];
int distances[NUMCARDS];
time_t t;

// functions

// Convert string to int calling strtol
int GetIntArg(char* str) {
	char* ptr;
	return strtol(str, &amp;ptr, 10);
}

// Merges two cards a and b. 50:50 chance that a above b or other way
void DoRiffle() {
	// Copy cards into tempCards
	memcpy(tempCards, cards, sizeof(cards));
	// Merge each pair of cards tempCards[i] and TempCards[i+26]
	int cardIndex = 0;
	for (int i = 0; i &lt; NUMCARDS / 2; i++) {
		if (rand() % 2) {
			cards[cardIndex++] = tempCards[i];
			cards[cardIndex++] = tempCards[i + 26];

		}
		else {
			cards[cardIndex++] = tempCards[i + 26];
			cards[cardIndex++] = tempCards[i];
		}
	}
}

// Works out how far cards have moved, added to distances
void CalculateDistances() {
	for (int i = 0; i &lt; NUMCARDS; i++) {
		int moved = abs(cards[i] - startPos[i]);
		if (moved &gt; distances[i])
			distances[i] = moved;
	}
}

void DoShuffles() {
	// Clear distances and init cards
	for (char i = 0; i &lt; NUMCARDS; i++) {
		distances[i] = 0;		
		cards[i] = i;
	}
    // do Numrounds  rounds
	for (int round =0;round&lt;NumRounds;round++){
		// Mark where the card starts
		for (char i = 0; i &lt; NUMCARDS; i++) {
			startPos[i] = cards[i];
		}
		// Do seven riffles
		for (int i = 0; i &lt; NUMRIFFLES; i++) {
			DoRiffle();
		}		
		CalculateDistances();
	}
	int furthest = 0;
	for (int i = 0; i &lt; NUMCARDS; i++) {
		printf(&quot;Distance[%d]=%d\n&quot;, i, distances[i]);
		if (distances[i]&gt; furthest) {
			furthest = distances[i];
		}
	}
	printf(&quot;furthest moved is %d\n&quot;, furthest);
}


int main(int argc, char* argv[]) {
	srand((unsigned)time(&amp;t));
	if (argc ==1 || argc== 2) {
		NumRounds = MAXROUNDS;
		if (argc == 2) {
			NumRounds = GetIntArg(argv[1]);
			printf(&quot;Numrounds = %d\n&quot;, NumRounds);
		}
		DoShuffles();
	}
	else {
		printf(&quot;Please supply 0 or 1 arguments e.g. riffle 60\n&quot;);
	}
}</code></pre>
<p>Even with ten rounds I&#8217;ve seen cards move 51 positions. With 5,000 rounds all cards but one moved by 51 and one by 50.</p>The post <a href="https://learncgames.com/how-to-calculate-how-effective-a-riffle-is/">How to calculate how effective a Riffle is</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">1325</post-id>	</item>
		<item>
		<title>How many riffles are needed to shuffle a deck?</title>
		<link>https://learncgames.com/how-many-riffles-are-needed-to-shuffle-a-deck/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-many-riffles-are-needed-to-shuffle-a-deck</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Tue, 01 Sep 2020 23:00:20 +0000</pubDate>
				<category><![CDATA[Techniques]]></category>
		<category><![CDATA[card]]></category>
		<category><![CDATA[riffle]]></category>
		<category><![CDATA[smoosh]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=1320</guid>

					<description><![CDATA[<p>It fascinates me because 52! is such a large number.  Here it is in full 8.0658* 10^67 or 80,658,175,170,943,878,571,660, 636,856,403,766,975,289,505,440,883, 277,824,000,000,000,000. That is the possible number of ways of shuffling a pack of cards. It means that when you shuffle a deck of cards, it&#8217;s possible that you are the first person on Earth to [&#8230;]</p>
The post <a href="https://learncgames.com/how-many-riffles-are-needed-to-shuffle-a-deck/">How many riffles are needed to shuffle a deck?</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<p><img decoding="async" class="alignleft size-medium wp-image-1321" src="https://learncgames.com/wp-content/uploads/2020/08/card-riffle-300x225.png" alt="card riffle photo" width="300" height="225" srcset="https://learncgames.com/wp-content/uploads/2020/08/card-riffle-300x225.png 300w, https://learncgames.com/wp-content/uploads/2020/08/card-riffle.png 640w" sizes="(max-width: 300px) 100vw, 300px" />It fascinates me because 52! is such a large number.  Here it is in full 8.0658* 10^<sup>67</sup> or 80,658,175,170,943,878,571,660, 636,856,403,766,975,289,505,440,883, 277,824,000,000,000,000. That is the possible number of ways of shuffling a pack of cards.</p>
<p>It means that when you shuffle a deck of cards, it&#8217;s possible that you are the first person on Earth to ever get that particular arrangement. It&#8217;s the kind of fact that amazes me. Another one is that it takes a very long time for particles emitted from the centre of the sun to reach the surface and blast into space. On the order of many many years. (<em>Thousands of years!</em>)</p>
<p>Playing cards have only been around maybe 500 years as we know it (<em>52 card deck</em>) though date back to 9th century China for their invention. If there had been a billion shuffles each day during that 500 years, that&#8217;s only 1.8 x 10^<sup>14</sup> shuffles. That is a minuscule fraction of the possible number of arrangements so the chances are that any shuffled arrangement is new is pretty high.</p>
<p>It&#8217;s accepted that seven is the number of riffles needed to perfectly shuffle a pack of cards. A riffle is where you split the deck in two and then merge the two halves back into one deck as in the photo I took.</p>
<p>I proved this once by writing a program to simulate riffles  and looking how far cards have moved after seven. In fact a card at the top of the deck moved to the bottom after only six riffles. I&#8217;ll try and write that in C and will publish it here in a day or two.</p>
<p>Other shuffling techniques like smooshing <em>(spreading out all the cards on the table with their backs face up and then pushing them together</em>) are nowhere near as efficient. It&#8217;s estimated it can take thousands of smooshes to properly shuffle a pack. It&#8217;s not easy to simulate, though one of these days I&#8217;ll have a go and see if I can come up with a more accurate estimation.</p>The post <a href="https://learncgames.com/how-many-riffles-are-needed-to-shuffle-a-deck/">How many riffles are needed to shuffle a deck?</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">1320</post-id>	</item>
	</channel>
</rss>
