472,353 Members | 972 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\tElement location: " << counter
<< endl;

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

Feb 19 '07 #1
4 9060
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
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
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
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.

### Similar topics

 23 by: JC | last post by: I am very new to programming and learning on my own. Why do I keep getting duplicate values using this code? I want to shuffle a deck of 52... 6 by: CaseyB | last post by: If I wanted to create a game like Solitaire that would first randomly shuffle a deck of cards, I figured out that all I had to use is the Random()... 10 by: Arun Nair | last post by: Can any one help me with this im not getting it even after reading books because there is not much of discussion anywhere a> Implement a calss... 3 by: JayP | last post by: I'm trying to shuffle a deck of card in C ++, with out having the same card twice. And I'm supposed to give each player 5 cards. And if they want,... 8 by: garyrowell | last post by: I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. ... 8 by: l1nuxxx | last post by: I have a file well call file.pl. It's a card sorting program. I need to create a lib fuction with part of the original file that shuffles the deck of... 0 by: Naresh1 | last post by: What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge... 0 by: jalbright99669 | last post by: Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made... 2 by: Matthew3360 | last post by: Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it... 0 by: Arjunsri | last post by: I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and... 0 by: WisdomUfot | last post by: It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific... 0 by: Matthew3360 | last post by: Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web... 0 by: Oralloy | last post by: Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the... 0 by: BLUEPANDA | last post by: At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS... 0 by: Rahul1995seven | last post by: Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...