2

Decided to create a deck in C++ using a vector and a Card class. I chose this because shuffling is supposed to be easy using random_shuffle. My first approach just to get aquatint with C++ I made a Card list, used vector to generate numbers and shuffle them just to insert the cards in a tmp list and return it. This was messy to say the least.

So on my second branch I tried to do it with a vector from the start, it looks like this:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

// Represent a card
class Card
{
private:
  int suit;
  int value;

public:
  Card(){}

  Card(int v, int s)
  {
    suit = s;
    value = v;
  }

  void
  setNewVals(int v, int s)
  {
    value = v;
    suit = s;
  }

  int
  getValue()
  {
    return value;
  }

  int
  getSuit()
  {
    return suit;
  }
};

// Shuffle deck
void
shuffleDeck(vector<Card> *c, int shuffles)
{
  for(int s = 0; s < shuffles; s++)
    {
      random_shuffle(c->begin(), c->end());
    }
}

int
main()
{
  //TODO: Later input from main
  int numDecks = 10;
  int numShuffles = 10;

  // Create a deck vector
  vector<Card> c;

  // Select card's value
  for(int d = 0; d < numDecks; d++)
    {
      for(int v = 1; v < 14; v++)
        {
          // Select card's suit
          for(int s = 0; s < 4; s++)
            {
              Card card(v, s);
              c.push_back(card);
            }
        }
    }

  // Shuffle the deck
  shuffleDeck(&c, numShuffles);

  // Print out first 52 cards
  for(int i = 0; i < 52; i++)
    {
      Card aCard = c.front();
      cout << aCard.getValue() << "\t" << aCard.getSuit() << endl;
      c.erase(c.begin());
    }

  return 0;
}

The problem is that now the random_shuffle does not really work as expected. It shuffles alright, but all cards always end up in the same order. When I did almost the exact same thing with int, it worked fine.

I really have no idea why it is not shuffling random all the time. Have I misinterpreted how vectors work?

Salviati
  • 758
  • 2
  • 9
  • 28
  • OP probably means that the cards shuffle, but each run of the program has them end up in the same order after shuffling – Tas Aug 30 '18 at 01:17
  • Everything is random, but if I quit the program and start it again, it shuffles them in the exact same order again. That is not random, is it? Ex, run ./main, print all cards out, thay are random. Exit and run ./main again, and it is shuffled in the exact same order. – Salviati Aug 30 '18 at 01:18
  • Exactly, updated to printing out 52 cards instead of one card if anyone want to confirm it or say I am wrong – Salviati Aug 30 '18 at 01:20
  • 2
    Maybe this will help? https://stackoverflow.com/questions/6931951/how-to-make-sure-that-stdrandom-shuffle-always-produces-a-different-result – GBlodgett Aug 30 '18 at 01:22

2 Answers2

3

You should see http://www.cplusplus.com/reference/algorithm/random_shuffle/ link.

The randomness of std::random_shuffle() seems depends on the std::srand function.

so, you should code like follows (It is just one example)

  #include <iostream>
  #include <algorithm>
  #include <vector>
  #include <ctime>
  ...
  int main() {
      ...
      // Make randomness for shuffle
      std::srand(unsigned(std::time(0)));

      // Shuffle the deck
      shuffleDeck(&c, numShuffles);
      ...
  }
2

In order for std::random_shuffle to return different values at different runs, it needs to be given a non-fixed seed.

You can solve this by adding the line:

std::srand(std::time(NULL));

Now executions at least 1 second apart will give different shuffling results.

Kostas
  • 4,061
  • 1
  • 14
  • 32