Shuffle Three Cards?
Every time I run the code, it gives me same result. What is wrong?
This is my code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <iostream>
#include <vector>
#include <algorithm>
const char CARDS[] = { 'K', 'Q', 'J'};
unsigned const CARDS_SIZE = sizeof(CARDS)/sizeof(char);
void printCards(std::vector<const char*>& cards);
int main()
{
std::vector<const char*> pCards;
for ( unsigned i = 0; i < CARDS_SIZE; ++i)
{
pCards.push_back(&CARDS[i]);
}
for ( unsigned i =0 ; i < 5; ++i)
{
std::random_shuffle(pCards.begin(), pCards.end());
printCards(pCards);
}
return 0;
}
void printCards(std::vector<const char*>& cards)
{
std::vector<const char*>::iterator it;
for ( it = cards.begin(); it != cards.end(); ++it)
{
std::cout << **it << " ";
}
std::cout << std::endl;
}
|
This is the output
K J Q
K J Q
J K Q
K J Q
K Q J |
@naraku9333,
thanks. it worked.
Topic archived. No new replies allowed.