473,807 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9195
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.

Similar topics

23
12963
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 cards. The logic seems right to me. Randomize For C = 0 To 1000 C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
6
6224
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() class or rnd and make sure I use the Randomize function so as not to get the same card twice. BUT I noticed that some Solitaire games allow you to select one of the 4,294,967,296 possibilities in a 52 card deck. I want to do something similar, after I shuffle the deck I want to show the number...
10
4968
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 that represents a playing card. The class should implement the following methods: _ _ init _ _ (self, rank, suit) Creates a card. rank is an integer in range of 1 to 13 (Ace:1, King 13), suit is a character in set {'h','d','c','s'} getRank(self) Returns the rank of the card
3
3062
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, they can replace 3 cards. Right now I can assign 5 cards to each player, but some of the cards repeat themselces (ex. I have 3 Ace of spades, or 2 king of clubs, etc). Can anyone help me fix this problem. Here is what I have so far. #include <iostream> #include <ctime> #include <cstdlib>...
8
29172
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. The program This week you are going to write three classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below. Card.Java This is a simple class that represents a playing card. Card has two attributes: • rank which is a String that represents the value...
8
5059
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 cards. After it shuffles the first deck and deals a hand of cards I need it to call the shuffling function again before dealing another hand. I call the lib function file file-lib.pl. here's the contents i have for file.pl #!/usr/bin/perl require 'file-lib.pl'; my @startingdeck = ("A...
0
9720
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10626
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10112
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.