473,549 Members | 2,556 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 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.

Similar topics

23
12912
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
6180
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...
10
4944
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...
3
3048
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...
8
29144
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...
8
5036
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...
0
7526
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...
0
7457
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
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. ...
1
7483
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7817
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...
0
6051
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...
1
5375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3504
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...
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.