473,549 Members | 2,556 Online

# Deck of Cards -- Shuffling (void shuffle)

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\tElemen t location: " << counter
<< endl;

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

Feb 19 '07 #1
4 9178
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\tElemen t location: " << counter
<< endl;

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

Feb 19 '07 #2
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\tElemen t 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
In article <11************ **********@k78g 2000cwa.googleg roups.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_shu ffle.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 19 '07 #4
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 thread has been closed and replies have been disabled. Please start a new discussion.