0

I need to seed a random function with a seed 2222. The shuffleDeck() function calls random_shuffle from the std library.

Note: The problem is, every time I run the program with no_deals=5, and cards_per_hand=5, I get the same pattern:

Jack of Spades, King of Diamonds, 10 of Hearts, 4 of Clubs, King of Spades, 8 of Diamonds, 3 of Diamonds, 3 of Diamonds, 7 of Hearts, 5 of Clubs, 3 of Hearts, 10 of Hearts, 6 of Diamonds, King of Spades, Jack of Diamonds, 7 of Hearts, 3 of Diamonds, King of Diamonds, Jack of Hearts, 3 of Diamonds,

Shouldn't there be a change? Am I inputting the seed 2222 correctly?

srand(2222);

for (int i=0; i<no_deals; i++)
{
   // test.shuffleDeck();
         for(int j=0; j<cards_per_hand; j++)
      {
                //test.dealCard();

                check.at(j)=test.dealCard();
                cout<<check.at(j)<<", ";
                test.shuffleDeck();
      }

       cout<<endl;

}
Anitej Rao
  • 25
  • 10
  • 3
    You are supposed to get the same pattern when you use the same seed. – Ahmed Masud Jan 31 '16 at 14:00
  • 2
    If you can use c++11, consider http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution over C random numbers, which are quite inferior – johnbakers Jan 31 '16 at 14:04

1 Answers1

3

That's how a psuedo-random generator works. Everytime you input the same seed it will output the same numbers in the same sequence.

To fix this, use time(NULL) instead:

srand(time(NULL));

And only call srand once in your program.

If you have access to C++11 you should really be using the new <random> header functionalities though, an example here.

Community
  • 1
  • 1
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122