More on Shuffledness

So I’ve updated my shuffledness calculation. This is a bit of a work in progress so probably isn’t the last version. One of the things I’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.
Obviously doing this 10 times would only shuffle at most 20 cards and probably fewer as there’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..
void ShuffleDeck(int numtries) {
int firstIndex, secondIndex;
for (int i = 0; i < numtries; i++) {
do {
firstIndex = rand() % 52;
secondIndex = rand() % 52;
}
while (firstIndex == secondIndex);
int value = deck[firstIndex];
deck[firstIndex] = deck[secondIndex];
deck[secondIndex] = value;
}
}
Anyway, I’ve added a CalcDistance() function. This looks at each card and calculates how far it has moved from it’s original position. It then divides this by the maximum distance it could have moved to get a move “factor”. These are summed up and divided by 52 to give an average move factor. That’s your CalcDistance() function.
float CalcDistance() {
float distance = 0.0f;
for (int i = 0; i < 52; i++) {
float distanceDiv = (i < 26) ? 51 - i : i-1;
//printf("%i: %3.0f\n", i, distanceDiv);
distance += (abs(deck[i] - i) / distanceDiv);
}
return distance/52.0f;
}
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.
This is the current state of the program which I’ve listed fully below. Enjoy! It’s 63 lines long.
// shuffledness.c : This measures how shuffled a deck of cards is
// cards are hjeld as values 0-51 in a 52 int aaray.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
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 < 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 < 52; i++) {
total += abs(deck[i] - deck[i - 1]);
}
printf("Total = %d\n", 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 < 52; i++) {
float distanceDiv = (i < 26) ? 51 - i : i-1;
//printf("%i: %3.0f\n", 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 < 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(&t));
for (int i = 0; i < 25; i++) {
InitDeck();
ShuffleDeck(1000);
printf("CalcDisorder() ==%f x %f = %f\n", CalcDisorder(),CalcDistance(),(double)CalcDisorder() * CalcDistance());
}
return 0;
}
Because I’m multiplying the two factors (disorder and Distance, the final value is never getting much above 0.35 and I’d prefer it to be in the range 0-0.999. Suggestions welcomed.
.
Those of you who have used the Putty utility to connect to a remote computer using SSH might recognise the name chiark. It’s also the 

This is somewhat off-topic and a little bit of self-indulgence because (a) it’s about a computer that existed over 36 years ago and (b) a game that was programmed in 6502 assembler. It’s also a game I played a lot back in 1984.
Oh I do play much newer games such as Far Cry 5 which I’ve completed recently but Quake II was a favourite of mine back in 1997 when I bought it. I’m more of a Quake than a Doom person. Unfortunately for me, my then PC couldn’t run it. It was only two years old as well. But I got a new PC in 1998 and that played it just fine. I’m not saying I’m a saddo but I can play it through on the hardest level without losing a life.

I’m using this in Android games but the principle applies to any MonoGame game. Here clickable and touchable mean the same.
To set the TouchArea of the ClearButton, I do it in the Draw method. It’s as simple as this:
This is what the Visual Studio debugger shows just before the final loop that calls free.