Anitej Rao
Anitej Rao

Reputation: 25

Seeding value for random function c++

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;

}

Upvotes: 0

Views: 129

Answers (1)

Hatted Rooster
Hatted Rooster

Reputation: 36463

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.

Upvotes: 3

Related Questions