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.
-
Sorry, I did forget to mention that I've done this with an array and would like to not use that (if possible). I am learning about structs so any way that would be relevant with that would be better.user2344665– user23446652014-02-16 00:45:08 +00:00Commented Feb 16, 2014 at 0:45
-
Roll a number between 0 and 3, and take C if 0, S if 1, and so on? You could put C,S,H and D into an array and roll its index. Or is that what you mean with "not using an array"?JiaYow– JiaYow2014-02-16 00:47:30 +00:00Commented Feb 16, 2014 at 0:47
-
first way would work, i would just need to make sure it's actually random. Any way to do that other than srand? (from what I've heard it's not very random). EDIT nevermind, that's for me to research :) thanks for the idea manuser2344665– user23446652014-02-16 00:50:05 +00:00Commented Feb 16, 2014 at 0:50
-
@user3308129 -- I don't understand why you would design the game around picking between suits. Wouldn't you want to pick a random card out of 52 (not including jokers)?PaulMcKenzie– PaulMcKenzie2014-02-16 00:51:11 +00:00Commented Feb 16, 2014 at 0:51
-
There are plenty of random generators for C++, some of them are cryptically secure. Google them, that's not the scope of this question.JiaYow– JiaYow2014-02-16 00:51:55 +00:00Commented Feb 16, 2014 at 0:51
|
Show 7 more comments
1 Answer
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/