Please, somebody help me with this program!
We have a deck of 52 cards and we shuffle the deck by choosing a
random card out of the deck placing it back in the deck at some random
place. Repeat it 500 times and then consider the deck shuffled. Choose
a hand of 5 cards from the top of the deck. Count the number of hands
which have threeofakind but don't count hands which have
fourofakind. Continue to draw new hands until the deck either needs
reshuffling or until 100 hands have been drawn. Output on one screen
the 100 handswith those having exactly threeofakind. Output also
the total amount of threeofakind hands.
Please I'm really confused. I know that I have to use functions and
the 52 cards are actually an array of 52 elements. But I don't know
how to do the shuffling drawind, the output of the 100 hands and
finally counting the hands wyh exactlu 3ofakind. I can't figure out
a method for testing a hand for exactly 3ofakind. Please help.
Thank you! 16 2013
"glowfire" writes: We have a deck of 52 cards and we shuffle the deck by choosing a random card out of the deck placing it back in the deck at some random place. Repeat it 500 times and then consider the deck shuffled. Choose a hand of 5 cards from the top of the deck. Count the number of hands which have threeofakind but don't count hands which have fourofakind. Continue to draw new hands until the deck either needs reshuffling or until 100 hands have been drawn. Output on one screen the 100 handswith those having exactly threeofakind. Output also the total amount of threeofakind hands.
Please I'm really confused. I know that I have to use functions and the 52 cards are actually an array of 52 elements. But I don't know how to do the shuffling drawind, the output of the 100 hands and finally counting the hands wyh exactlu 3ofakind. I can't figure out a method for testing a hand for exactly 3ofakind. Please help.
The first thing you must do is create a deck of 52 cards. The requirement
for threeofakind madates that the card needs to have both a value and
suit. I suggest this structure for a single card.
struct Card
{
int nbr; // 0..51
int suit; // 0..3
int pip; // 0..12
};
Given nbr you can compute suit and pip using the modulo operator.
Try creating the code to create a card deck and post the code. If you do,
someone will probably help you to the next step. You will probably want to
focus on the for statement and the modulo operator.
Later on, you will need to be able to generate random numbers in the range
0..51. There is a lot of guidance on the Web for generating random numbers
and also for shuffling.
glowfire wrote: Please, somebody help me with this program!
We have a deck of 52 cards and we shuffle the deck by choosing a random card out of the deck placing it back in the deck at some random place. Repeat it 500 times and then consider the deck shuffled. Choose a hand of 5 cards from the top of the deck. Count the number of hands which have threeofakind but don't count hands which have fourofakind. Continue to draw new hands until the deck either needs reshuffling or until 100 hands have been drawn. Output on one screen the 100 handswith those having exactly threeofakind. Output also the total amount of threeofakind hands.
Please I'm really confused. I know that I have to use functions and the 52 cards are actually an array of 52 elements. But I don't know how to do the shuffling drawind, the output of the 100 hands and finally counting the hands wyh exactlu 3ofakind. I can't figure out a method for testing a hand for exactly 3ofakind. Please help. Thank you!
I consider the following pseudocode more efficient and with better
shuffling.
// One pass shuffling
for(unsigned i=0; i<52; ++i)
cards[i]= cards[ Random(0,51) ];
Since it is a homework you can figure out how to do the Random(0, 51) by
using rand(), RAND_MAX and at first with a call to srand((time(0)).
Unless the homework requires the silly shuffling that was mentioned.

Ioannis Vranos http://www23.brinkster.com/noicys
On 20050219 21:53:49 0500, Ioannis Vranos <iv*@remove.this.grad.com> said: glowfire wrote:
Please, somebody help me with this program!
We have a deck of 52 cards and we shuffle the deck by choosing a random card out of the deck placing it back in the deck at some random place. Repeat it 500 times and then consider the deck shuffled. Choose a hand of 5 cards from the top of the deck. Count the number of hands which have threeofakind but don't count hands which have fourofakind. Continue to draw new hands until the deck either needs reshuffling or until 100 hands have been drawn. Output on one screen the 100 handswith those having exactly threeofakind. Output also the total amount of threeofakind hands.
Please I'm really confused. I know that I have to use functions and the 52 cards are actually an array of 52 elements. But I don't know how to do the shuffling drawind, the output of the 100 hands and finally counting the hands wyh exactlu 3ofakind. I can't figure out a method for testing a hand for exactly 3ofakind. Please help. Thank you! I consider the following pseudocode more efficient and with better shuffling.
// One pass shuffling for(unsigned i=0; i<52; ++i) cards[i]= cards[ Random(0,51) ];
Very bad idea, you can get copies of the same card in multiple places
in the array, and lose other cards. I'd recommend using
std::random_shuffle() instead.

Clark S. Cox, III cl*******@gmail.com
Ioannis Vranos wrote:
More clearly what I meant: I consider the following pseudocode more efficient and with better shuffling.
// One pass shuffling for(unsigned i=0; i<52; ++i)\
{
random_index= Random(0,51);
temp= cards[i];
cards[i]= cards[ random_index ];
cards[ random_index ]= temp;
}

Ioannis Vranos http://www23.brinkster.com/noicys
Clark S. Cox III wrote: Very bad idea, you can get copies of the same card in multiple places in the array, and lose other cards. I'd recommend using std::random_shuffle() instead.
I posted as a reply to my message what I meant in more detail.
BTW your message looks strange in my newsreader with Japanese encoding.
Unless you are also posting to Japanese groups, a good idea would be to
switch to Western(ISO) as the default.

Ioannis Vranos http://www23.brinkster.com/noicys
"Ioannis Vranos" wrote: Ioannis Vranos wrote:
More clearly what I meant:
I consider the following pseudocode more efficient and with better shuffling.
// One pass shuffling for(unsigned i=0; i<52; ++i)\ { random_index= Random(0,51);
temp= cards[i];
cards[i]= cards[ random_index ];
cards[ random_index ]= temp; }
No matter what you meant, it is still a very bad idea. Very few instructors
like being told that their way is stupid, and that is what both of the two
solutions in this thread imply. He especially will not like it coming from
a struggling student, and the OP is clearly struggling. He should do what he
has been told to do!!! By the *instructor*.
Clearly, the instructor has some intuitive notions of randomness that are
flawed. So be it, even so his way will still work pretty well and *he* is
the one who is going to give a grade.
If he had wanted a canned routine such as shuffle he would have permitted
it. He clearly excluded it. Some people actually learn something from
writing code that has already been written. I think the instructor had this
in mind.
To the OP: BTW, if you post again tell us whether you are supposed to use
classes or not. The assignment can be completed either way and we have no
way of knowing what the instructor expects. If you have a choice I think
without classes is easier. It is certainly less typing. Note that easier
does not mean better.
"osmium" <r1********@comcast.net> wrote in message
news:37*************@individual.net... "Ioannis Vranos" wrote:
Ioannis Vranos wrote:
More clearly what I meant:
I consider the following pseudocode more efficient and with better shuffling.
// One pass shuffling for(unsigned i=0; i<52; ++i)\ { random_index= Random(0,51);
temp= cards[i];
cards[i]= cards[ random_index ];
cards[ random_index ]= temp; }
No matter what you meant, it is still a very bad idea. Very few
instructors like being told that their way is stupid, and that is what both of the two solutions in this thread imply. He especially will not like it coming
from
<<snip>>
If I recall, this is exactly what the sort algorithm implements. I know for
a fact it is the one in Java (except it only does i from 0 to 51). I'll take
a peek at the g++ sort and see.

Gary
"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:nJ********************@comcast.com... "osmium" <r1********@comcast.net> wrote in message
The description from dinkumware for random_shuffle is
The first template function evaluates swap(*(first + N), *(first + M)) once
for each N in the range [1, last  first), where M is a value from some
uniform random distribution over the range [0, N]. Thus, the function
randomly shuffles the order of elements in the sequence.
This would appear to be the same as the Java implementation. In the STL the
sort is implemented with iterators, of course. In stl_algo.h (g++ 3.4.2) we
find
if (__first != __last)
for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
std::iter_swap(__i, __first + (std::rand() % ((__i  __first) + 1)));
Which is the same algorithm. Looks like everyone is doing it.
I have my students program it exactly that way in Java class.

Gary
Gary Labowitz wrote: The description from dinkumware for random_shuffle is The first template function evaluates swap(*(first + N), *(first + M)) once for each N in the range [1, last  first), where M is a value from some uniform random distribution over the range [0, N]. Thus, the function randomly shuffles the order of elements in the sequence.
This would appear to be the same as the Java implementation. In the STL the sort is implemented with iterators, of course. In stl_algo.h (g++ 3.4.2) we find
if (__first != __last) for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) std::iter_swap(__i, __first + (std::rand() % ((__i  __first) + 1))); Which is the same algorithm. Looks like everyone is doing it.
I have my students program it exactly that way in Java class.
random_shuffle can be used in such shuffle cases.
BTW osmium is right, the OP should stick with his assignment requirements.
The one you are providing above, is an implementation of random_shuffle?

Ioannis Vranos http://www23.brinkster.com/noicys
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1108954230.35731@athnrd02... Gary Labowitz wrote:
<<snip>> The one you are providing above, is an implementation of random_shuffle?
Yes, this is out of random_shuffle on g++ library.

Gary
Gary Labowitz wrote: Yes, this is out of random_shuffle on g++ library.
OK, in any case an implementer may choose whatever implementation he wants.

Ioannis Vranos http://www23.brinkster.com/noicys
Gary Labowitz wrote: This would appear to be the same as the Java implementation. In the STL the sort is implemented with iterators, of course. In stl_algo.h (g++ 3.4.2) we find
if (__first != __last) for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) std::iter_swap(__i, __first + (std::rand() % ((__i  __first) + 1))); Which is the same algorithm. Looks like everyone is doing it.
I have my students program it exactly that way in Java class.
Actually I think it is not exactly the same algorithm with what I
provided, I begin from the [0] element while the above from the [1].

Ioannis Vranos http://www23.brinkster.com/noicys
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1108868048.258131@athnrd02... glowfire wrote:
I consider the following pseudocode more efficient and with better shuffling.
// One pass shuffling for(unsigned i=0; i<52; ++i) cards[i]= cards[ Random(0,51) ];
After each assignment you have two copies of the same "card" in
two array positions [except occasionally when Random(0,51)==i].
That is not what I understand the word "shuffling" usually means.
 Risto 
unshuffled, alas
#include <iostream>
#include <string>
int main()
{
string suits = "\x5\x4\x3\x6";
string cards = "23456789TJQKA";
for(int a=0;a<suits.size();a++)
{
for(int b=0;b<cards.size();b++)
{
cout<<cards[b];
cout<<suits[a];
cout<<' ';
}
cout<<endl;
}
return 0;
}
//shuffled, dealt and sorted per hand
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
int main()
{
vector<string>deck;
vector<string>north;
vector<string>east;
vector<string>south;
vector<string>west;
string suits="\x5\x4\x3\x6";
string cards="23456789TJQKA";
for(int a=0;a<cards.size();a++)
for(int b=0;b<suits.size();b++)
{
string c;
c+=suits[b];
c+=cards[a];
deck.push_back(c);
}
random_shuffle(deck.begin(),deck.end());
while(deck.size()>0)
{
north.push_back(deck.back());deck.pop_back();
east.push_back(deck.back());deck.pop_back();
south.push_back(deck.back());deck.pop_back();
west.push_back(deck.back());deck.pop_back();
}
sort(north.begin(),north.end());
sort(east.begin(),east.end());
sort(south.begin(),south.end());
sort(west.begin(),west.end());
copy(north.begin(),north.end(),ostream_iterator<st ring>(cout," "));
cout<<endl;
copy(east.begin(),east.end(),ostream_iterator<stri ng>(cout," "));
cout<<endl;
copy(south.begin(),south.end(),ostream_iterator<st ring>(cout," "));
cout<<endl;
copy(west.begin(),west.end(),ostream_iterator<stri ng>(cout," "));
cout<<endl;
return 0;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paul CT 
last post by:
Hi,
Is there a way to repeat a set of code until a certain if statement is
satisfied. If it is then to exit the loop and if not repeat the code?...

by: Jennie Friesen 
last post by:
Hello
I would like to display a different line of text (32 different
messages) on refresh, BUT with no repeats.
The script I am currently...

by: Thomas Mlynarczyk 
last post by:
I remember there is a programming language where you can initialize the
random number generator, so that it can  if you want  give you the exactly...

by: steflhermitte 
last post by:
Dear cppians,
I want to apply a stratified sampling on an image. Following simplified
example will explain my problem.
The original image I...

by: Stu Banter 
last post by:
Hi,
On a previous question someone recommended using the
System.Security.Cryptography to fill an array with strong random bytes.
It works but...

by: jar13861 
last post by:
I'm confused on how to write a random array that will only generate 9
different numbers from 19. Here is what I have, but its only writing
one...

by: Erich Pul 
last post by:
hi!
i got a structure, which should be filled with random integer values
(it is in fact a generator for numbers like in a lotto), but these...

by: Pascal 
last post by:
hello and soory for my english
here is the query :"how to split a string in a random way"
I try my first shot in vb 2005 express and would like to...

by: caca 
last post by:
Hello,
This is a question for the best method (in terms of performance
only) to choose a random element from a list among those that satisfy
a...

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

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

by: AndyPSV 
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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

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

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

by: Carina712 
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

by: BLUEPANDA 
last post by:
At BluePanda Dev, we're passionate about building highquality software and sharing our knowledge with the community. That's why we've created a SaaS...

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