By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

Deck of Cards -- Shuffling (void shuffle)

P: n/a
For the time being, I'm doing a simple swap method for my deck of
cards using the random number generator in cstdlib. I've created a
dynamic array of a type class Card. When I go to shuffle it and then
later print the shuffled deck, some of the values overlap -- I can't
figure out why. Here's my code:

void shuffle()
{
for (counter = 0; counter < 120; counter++)
{
randNum1 = (rand() % 50);
randNum2 = (rand() % 50);
Temp[0].value = 0;
Temp[0].type = "";

cout << "Random Generated Number Is: " << randNum1 << " " <<
randNum2 << endl;

Temp[0] = Deck[randNum1];
Deck[randNum2] = Deck[randNum1];
Deck[randNum1] = Temp[0];
}

for (counter = 0; counter < 52; counter++)
{

cout << "Point Value: " << Deck[counter].value << "\t\t Type: "
<< Deck[counter].type << "\t\tElement location: " << counter
<< endl;

//for (long int i = 0; i <= 90000001; i++)
//;
}
};

Feb 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Sorry, but the code for setting the random variables... randNum1 and
randNum2 should equal what I have there, PLUS ONE.

On Feb 19, 12:06 am, "Pratik" <prpan...@gmail.comwrote:
For the time being, I'm doing a simple swap method for my deck of
cards using the random number generator in cstdlib. I've created a
dynamic array of a type class Card. When I go to shuffle it and then
later print the shuffled deck, some of the values overlap -- I can't
figure out why. Here's my code:

void shuffle()
{
for (counter = 0; counter < 120; counter++)
{
randNum1 = (rand() % 50);
randNum2 = (rand() % 50);
Temp[0].value = 0;
Temp[0].type = "";

cout << "Random Generated Number Is: " << randNum1 << " " <<
randNum2 << endl;

Temp[0] = Deck[randNum1];
Deck[randNum2] = Deck[randNum1];
Deck[randNum1] = Temp[0];
}

for (counter = 0; counter < 52; counter++)
{

cout << "Point Value: " << Deck[counter].value << "\t\t Type: "
<< Deck[counter].type << "\t\tElement location: " << counter
<< endl;

//for (long int i = 0; i <= 90000001; i++)
//;
}
};

Feb 19 '07 #2

P: n/a
On Feb 19, 12:09 am, "Pratik Pandey" <prpan...@gmail.comwrote:
Sorry, but the code for setting the random variables... randNum1 and
randNum2 should equal what I have there, PLUS ONE.

On Feb 19, 12:06 am, "Pratik" <prpan...@gmail.comwrote:
For the time being, I'm doing a simple swap method for my deck of
cards using the random number generator in cstdlib. I've created a
dynamic array of a type class Card. When I go to shuffle it and then
later print the shuffled deck, some of the values overlap -- I can't
figure out why. Here's my code:
void shuffle()
{
for (counter = 0; counter < 120; counter++)
{
randNum1 = (rand() % 50);
randNum2 = (rand() % 50);
Temp[0].value = 0;
Temp[0].type = "";
cout << "Random Generated Number Is: " << randNum1 << " " <<
randNum2 << endl;
Temp[0] = Deck[randNum1];
Deck[randNum2] = Deck[randNum1];
Deck[randNum1] = Temp[0];
}
for (counter = 0; counter < 52; counter++)
{
cout << "Point Value: " << Deck[counter].value << "\t\t Type: "
<< Deck[counter].type << "\t\tElement location: " << counter
<< endl;
//for (long int i = 0; i <= 90000001; i++)
//;
}
};

Ah, after a few hours of coding, your eyes can play tricks on you. My
swap method was incorrect. I fixed it now.

Feb 19 '07 #3

P: n/a
In article <11**********************@k78g2000cwa.googlegroups .com>,
pr******@gmail.com says...
For the time being, I'm doing a simple swap method for my deck of
cards using the random number generator in cstdlib. I've created a
dynamic array of a type class Card. When I go to shuffle it and then
later print the shuffled deck, some of the values overlap -- I can't
figure out why.
Having fixed your swapping, take a look at std::random_shuffle.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 19 '07 #4

P: n/a
On Sun, 18 Feb 2007 21:06:40 -0800, Pratik wrote:
For the time being, I'm doing a simple swap method for my deck of
cards using the random number generator in cstdlib. I've created a
dynamic array of a type class Card. When I go to shuffle it and then
later print the shuffled deck, some of the values overlap -- I can't
figure out why. Here's my code:
You're swapping a random position with a random position. How many times
must you do this, to ensure a full shuffle. Are you sure that 120 is
enough? How many cards will not be moved, after 120 random selections?

The more traditional approach is to loop on the cards in sequence,
swapping each with a random card from the remainder of the deck.

for (int i=0; i<51; i++)
{
int j = rand() % (51 - i) + i + 1;
card t = deck[i];
deck[i] = deck[j];
deck[j] = t;
}
--
Purgamentum init, exit purgamentum.

Feb 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.