Hey, I am not stuck on this or anything just a bit confused about if this is the right way to go..

Well i was wondering on the river (five cards that go into the middle of the table) i am going to make them random and that is no problem just the way i am thinking of is quite long..

Would doing something like this be ok? (In ok i mean is it one of the best, If not please correct)

riverCard1 = rand();

if(riverCard1 == 1) cout << "Ace Of Hearts" << " ";
else if(riverCard1 == 2) cout << "1 Of Hearts" << " ";

// .. etc etc etc

Dani AI

Generated

Short version: avoid a huge if/else mapping of random numbers to card names. Treat the deck as a clean data structure (52 distinct items), shuffle it, then draw the top 5. This follows 's class idea and builds on the randomness/seed points raised by and the duplicate-card caution from .

Example pattern (modern C++): create a deck as 52 integers 0..51, initialize once, shuffle with a good RNG, then pop cards to draw. Convert an id to rank/suit by rank = id % 13 and suit = id / 13; format names from small string tables. A compact Deck class can hold the vector, an std::mt19937 RNG and methods shuffle() and draw().

#include <vector>
#include <algorithm>
#include <random>
#include <numeric>
#include <string>

struct Deck {
    std::vector<int> d;
    std::mt19937 rng;
    Deck(unsigned seed = std::random_device{}()) : d(52), rng(seed) {
        std::iota(d.begin(), d.end(), 0);
    }
    void shuffle() { std::shuffle(d.begin(), d.end(), rng); }
    int draw() { int c = d.back(); d.pop_back(); return c; }
};

static const char* rankNames[13] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
static const char* suitNames[4] = {"Hearts","Diamonds","Clubs","Spades"};
std::string toString(int id) { return std::string(rankNames[id % 13]) + " of " + suitNames[id / 13]; }

Tips and cautions: shuffle once to avoid duplicates and expensive collision checks; use a fixed seed while debugging to reproduce hands, and seed from std::random_device for production randomness (or the OS crypto API if real money is involved). For Texas Hold'em, remember community cards are dealt as flop (3), turn (1), then river (1) so draw accordingly. This keeps logic simple, fast, and maintainable compared to many per-card if/elses.

Recommended Answers

All 3 Replies

Yes. You'd need to limit rand to the amount of cards in a deck.
You'd typically use a switch statement instead of the if/else's there. You'll need a for loop to check if the card has already been drawn.
Btw, the river is the fifth card dealt not the name for all 5.

To make rand() generate a random number inbetween a certain value you need to do something like this:

int var = rand() % 100; // Generates a random number between 0 and 100

But every time you load the program up you should first call srand(time(NULL));

A class called 'card', which knows about suit and rank.
A class called 'deck', with 52 cards in it.

'deck' has methods like 'shuffle' and 'draw'.

Call shuffle, then a loop to call draw 5 times.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.