title: How to generate 2,598,960 hands (efficient way) for poker game
body: <p>The total number of poker hands (i.e. five cards for each hand) from a 52 deck is computed by using the choose method nPk (i.e. nPk = n!/(n-k)!*k!) which is 2,598,960 possible hands. What I would like to do is to generate all possible cases(i.e. 2,598,960 hands). The only way I think of is to randomly generate a hand and add it to a vector. Every time I add a hand, I check it if it already exists if so, keep generate random hand. This will of course work but I'm looking for faster and elegant approach for this. The order of cards in a hand is not important. This is a minimal working example to save your time.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;ctime&gt;
  using namespace std;
  
  const int SUIT_MAX(4);
  const int RANK_MAX(13);
  const int HAND_SZ(5);
  const std::string SUIT[SUIT_MAX]  = {&quot;S&quot;, &quot;H&quot;, &quot;D&quot;, &quot;C&quot;};
  const std::string RANK[RANK_MAX]  = {&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;,&quot;10&quot;,&quot;J&quot;,&quot;Q&quot;,&quot;K&quot;,&quot;A&quot;};
  
  class Card
  {
  public:
      Card(const string &amp;suit, const string &amp;rank) { m_card = rank + suit; }
      std::string print() const { return m_card; }
  private:
      string m_card;
  };
  
  class Hand
  {
  public:
      Hand(const vector&lt;Card&gt;&amp; cards) 
      { 
          m_hand.push_back(cards);
      }
      void printHand()
      {
          for (int i(0); i &lt; m_hand.size(); ++i)
              for (int j(0); j &lt; m_hand[i].size(); ++j)
                  cout &lt;&lt; m_hand[i][j].print() &lt;&lt; &quot; &quot;;
          cout &lt;&lt; endl;
      }
  private:
      vector&lt;vector&lt;Card&gt;&gt; m_hand;
  };
  
  int main()
  {
      srand((int) time(0));
      Card c1(SUIT[0], RANK[3]);
      vector&lt;Card&gt; handVec;
      
      for(int i(0); i &lt; HAND_SZ; ++i){
          int suit = (rand() % (SUIT_MAX));
          int rank = (rand() % (RANK_MAX));
          Card c(SUIT[suit], RANK[rank]);
          handVec.push_back(c);
      }
          
      Hand h1(handVec);
      h1.printHand();
      
      return 0;
  }
  </code></pre>