user2344665
user2344665

Reputation:

C++ random fixed set of strings

So creating a poker game and I have a random number generator set up, now just want to generate a random string between "C", "S", "H", and "D" (obvious reasons). I looked a bit on SO and did not find exactly what I'm looking for, I did find a random string generator in C, but it was out of all of the letters, and you could not make it any more specific. I would post code that I have tried, but I haven't even gotten a foundation for how this will work.

Upvotes: 0

Views: 210

Answers (1)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

If you want to create a poker game, first define the struct that has both suit and number:

#include <string>
#include <vector>

struct Card
{
    int number;
    std::string suit;
};
//...
std::vector<Card> MyCards(52);
// write some code to initialize each card with their values
//...

Now when you have this, then this is how you shuffle the cards:

#include <algorithm>
//...
std::random_shuffle(MyCards.begin(), MyCards.end());

There is a 3 argument version of random_shuffle() that takes a custom random generator if you don't like the default.

http://www.cplusplus.com/reference/algorithm/random_shuffle/

Upvotes: 2

Related Questions