Difference between revisions of "Card Shuffling"

From Public Wiki
Jump to navigation Jump to search
Line 79: Line 79:
 
n[51] = "King of diamonds";
 
n[51] = "King of diamonds";
  
//Spice up the random function.
+
//Initialize the random function.
 
srand((unsigned) time(&t));
 
srand((unsigned) time(&t));
  

Revision as of 02:10, 22 December 2020

Synopsis

Define a deck of cards in an array. Shuffle these cards in O(n) time using modulus arithmetic

Notes

This code is specifically designed to leave an occasional card in it's original location.

Code

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<time.h>
  4 
  5 #define NUM_CARDS 52
  6 
  7 int main(void)
  8 {
  9 	short counter_a;
 10 	short d;
 11 	time_t t;
 12 	char a[NUM_CARDS];
 13 	char c;
 14 
 15 	const char *n[NUM_CARDS];
 16 	
 17 
 18 	n[0] = "Ace of spades";
 19 	n[1] = "2 of spades";
 20 	n[2] = "3 of spades";
 21 	n[3] = "4 of spades";
 22 	n[4] = "5 of spades";
 23 	n[5] = "6 of spades";
 24 	n[6] = "7 of spades";
 25 	n[7] = "8 of spades";
 26 	n[8] = "9 of spades";
 27 	n[9] = "10 of spades";
 28 	n[10] = "Jack of spades";
 29 	n[11] = "Queen of spades";
 30 	n[12] = "King of spades";
 31 	n[13] = "Ace of hearts";
 32 	n[14] = "2 of hearts";
 33 	n[15] = "3 of hearts";
 34 	n[16] = "4 of hearts";
 35 	n[17] = "5 of hearts";
 36 	n[18] = "6 of hearts";
 37 	n[19] = "7 of hearts";
 38 	n[20] = "8 of hearts";
 39 	n[21] = "9 of hearts";
 40 	n[22] = "10 of hearts";
 41 	n[23] = "Jack of hearts";
 42 	n[24] = "Queen of hearts";
 43 	n[25] = "King of hearts";
 44 	n[26] = "Ace of clubs";
 45 	n[27] = "2 of clubs";
 46 	n[28] = "3 of clubs";
 47 	n[29] = "4 of clubs";
 48 	n[30] = "5 of clubs";
 49 	n[31] = "6 of clubs";
 50 	n[32] = "7 of clubs";
 51 	n[33] = "8 of clubs";
 52 	n[34] = "9 of clubs";
 53 	n[35] = "10 of clubs";
 54 	n[36] = "Jack of clubs";
 55 	n[37] = "Queen of clubs";
 56 	n[38] = "King of clubs";
 57 	n[39] = "Ace of diamonds";
 58 	n[40] = "2 of diamonds";
 59 	n[41] = "3 of diamonds";
 60 	n[42] = "4 of diamonds";
 61 	n[43] = "5 of diamonds";
 62 	n[44] = "6 of diamonds";
 63 	n[45] = "7 of diamonds";
 64 	n[46] = "8 of diamonds";
 65 	n[47] = "9 of diamonds";
 66 	n[48] = "10 of diamonds";
 67 	n[49] = "Jack of diamonds";
 68 	n[50] = "Queen of diamonds";
 69 	n[51] = "King of diamonds";
 70 
 71 	//Initialize the random function.
 72 	srand((unsigned) time(&t));
 73 
 74 	//initialize with unsorted numbers
 75 	for(counter_a=0;counter_a<NUM_CARDS;counter_a++)
 76 	{	
 77 		a[counter_a] = counter_a;
 78 	}
 79 
 80 	counter_a--;
 81 	while(counter_a>0)
 82 	{
 83 		//if statement, because sometimes we don't want to
 84 		//always swap a card.  The larger the comparison
 85 		//number, the more frequently it is that a pair
 86 		//isn't swapped
 87 		
 88 		if(rand() % (NUM_CARDS)>2)
 89 		{
 90 			d=rand() % counter_a;
 91 			c=a[counter_a];
 92 			a[counter_a]=a[d];
 93 			a[d]=c;
 94 		}
 95 		printf("I just drew a %s\n", n[a[counter_a]]);
 96 		counter_a--;
 97 	}
 98 	printf("Out of cards, I'm done !\n");
 99 	return 0;
100 }