To deal the cards you should simply take the top card after shuffling.
If you want to select a random item anyway (or without shuffling) you can do that by selecting a random index in the range, and then removing that item from the array. To select a second item you again choose a random index (in the now reduced range) and then remove that item as well.
Also, your shuffle algorithm is not correct. It looks like you attempted to address the usual problem (why does this simple shuffle algorithm produce biased results? what is a simple reason?) but there's a typo. Your code int r = i + (rand() % (52-1))
should be int r = i + (rand() % (52-i))
. 52-i
, not 52-1
. The bug will cause you to potentially access outside the array bounds. I think your card swapping code also has a typo.
And of course rand()
is generally a poor source of random data. The <random>
library is better and easier to use.
I'll show two versions of an example solution, one using <random>
and one using rand()
. <random>
is what you should do, but I show rand()
simply to keep the differences from your code to a minimum.
#include <random>
std::vector<int> cards(52);
// fill the array
std::iota(std::begin(cards), std::end(cards), 0);
// std::shuffle(std::begin(cards), std::end(cards), eng);
std::default_random_engine eng;
std::uniform_int_distribution<int> dist;
// select 10 cards
for (int i=0; i<10; ++i) {
// select a random index in the range [0, cards.size()-1]
int card_index = dist(eng, {0, cards.size()-1});
int card = cards[card_index]; // get the card and do something with it
cards.erase(std::begin(cards) + card_index); // remove the card from the deck
}
int n = 52;
int cards[52];
// select 10 cards
for (int i=0; i<10; ++i) {
int card_index = rand() % n;
int card = cards[card_index]; // get the card
// remove the card from the deck
--n;
card[card_index] = card[n];
}