This is my code so far. The shuffle() function is not really supposed to print the deck but i did that for debugging purposes, so i could check how the deck got shuffled.Code:#include <iostream> #include <ctime> #define LOW 2 #define HIGH 52 void Swap(unsigned int &A, unsigned int &B) { int C = A; A = B; B = C; } class Deck { private: unsigned int Cards[52][2]; time_t Seconds; unsigned int DeckSize; public: Deck(); void Shuffle(); }; Deck::Deck() { DeckSize = 52; time(&Seconds); srand((unsigned int) Seconds); unsigned int Values[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; int i = 0, j = 0 , k = 1; for(i; i < 52; i++) { Cards[i][0] = Values[j]; (j == 12) ? j = 0 : j++; } for(i = 0; i < 52; i++) { Cards[i][1] = k; (j == 12) ? (k++, j=0) : j++; } } void Deck::Shuffle() { DeckSize = 52; int i = 52, RandomIndex = 0; for(i; i >= 2; i--) { RandomIndex = (rand() % (HIGH - LOW + 1) + LOW) % i; Swap(Cards[i][0], Cards[RandomIndex][0]); Swap(Cards[i][1], Cards[RandomIndex][1]); } for(i = 0; i < 52; i++) { std::cout << Cards[i][0] << " " << Cards[i][1] << std::endl; } } int main() { Deck Foo; Foo.Shuffle(); std::cin.get(); }

My deck is as you can see a 2 dimensional array of 52x2 unsigned integers. The first row is the Face Value of the card (Jack, Ace, 5 etc.), the second row is the suit of the card (Diamonds, Clubs and so on).

The algorithm i used loops through the deck from 52 to 2. It gets a random number (mod the loop index), and swaps the corresponding element in the array with the element of the index loop.

The output i get is almost what i wanted, the deck is shuffled, quite randomly. But one of the elements contains a huge number, instead of 2-14, and the suit element of that card contains 52 (or MAX). The number i get looks like an overflow error, but i've gone over the loops a million times, with no results.

1. Is my shuffling algorithm valid, or is there a better one i can use?

2. Where is the overflow error, i for sure as hell can't find it, and i'ts been bugging me for hours!