473,714 Members | 2,587 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

random shuffling

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 three-of-a-kind but don't count hands which have
four-of-a-kind. 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 three-of-a-kind. Output also
the total amount of three-of-a-kind 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 3-of-a-kind. I can't figure out
a method for testing a hand for exactly 3-of-a-kind. Please help.
Thank you!
Jul 23 '05 #1
16 2117
"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 three-of-a-kind but don't count hands which have
four-of-a-kind. 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 three-of-a-kind. Output also
the total amount of three-of-a-kind 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 3-of-a-kind. I can't figure out
a method for testing a hand for exactly 3-of-a-kind. Please help.


The first thing you must do is create a deck of 52 cards. The requirement
for three-of-a-kind 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.
Jul 23 '05 #2
Check out this nifty program. It does exactly what you are trying to do!!
I'm trying to work with it myself to make a card game.
http://home.comcast.net/~anglewyrm/hat.html
Jul 23 '05 #3
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 three-of-a-kind but don't count hands which have
four-of-a-kind. 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 three-of-a-kind. Output also
the total amount of three-of-a-kind 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 3-of-a-kind. I can't figure out
a method for testing a hand for exactly 3-of-a-kind. 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
Jul 23 '05 #4
On 2005-02-19 21:53:49 -0500, Ioannis Vranos <iv*@remove.thi s.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 three-of-a-kind but don't count hands which have
four-of-a-kind. 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 three-of-a-kind. Output also
the total amount of three-of-a-kind 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 3-of-a-kind. I can't figure out
a method for testing a hand for exactly 3-of-a-kind. 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_shu ffle() instead.

--
Clark S. Cox, III
cl*******@gmail .com

Jul 23 '05 #5
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
Jul 23 '05 #6
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_shu ffle() 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
Jul 23 '05 #7
"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.
Jul 23 '05 #8
"osmium" <r1********@com cast.net> wrote in message
news:37******** *****@individua l.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
Jul 23 '05 #9
"Gary Labowitz" <gl*******@comc ast.net> wrote in message
news:nJ******** ************@co mcast.com...
"osmium" <r1********@com cast.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 (_RandomAccessI terator __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
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3146
by: Paul C-T | 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? Say I want to write a card game application in PHP and I want to chose a card from the deck $card1 = rand(1,52); gets me my first card. I need to record which card has been dealt so I have a variable $dealt which starts out as a string of 52 zeros. I update the position of $card1 in the
3
8825
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 using is: <script language=javascript type=text/javascript> <!-- a = Math.floor(Math.random() * 32);
23
4189
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 same sequence of random numbers every time you initialize it with the same parameter. Can this be done with JavaScript? I couldn't find anything in the documentation. Basically, what I want to achieve is to obtain always the same sequence of random numbers for the same given initialization...
1
4718
by: steflhermitte | last post by:
Dear cpp-ians, I want to apply a stratified sampling on an image. Following simplified example will explain my problem. The original image I with nrows and ncols is now a vector V of length (nrow x ncol) and every element of the vector contians its pixel value. vector float V ;
9
4371
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 I can't specify the max value of course.. I solved it by subsequently removing all 'higher than max' values, but this is not efficient, nor fast. Anyone who can point me to a C#-usable Random Number Generator with at least
5
2079
by: jar13861 | last post by:
I'm confused on how to write a random array that will only generate 9 different numbers from 1-9. Here is what I have, but its only writing one number.... holder = new Array ( 9 ); var flag = true; var rannum = Math.floor( 1 + Math.random() * 9 ); for (var j = 0; j < 9; j++) {
19
4217
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 values must not be recurring, so no double occurrences are desired. my code: <code> Tipp* ziehung() // Tipp* is the function
12
3223
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 split a number in several pieces in a random way without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
15
2728
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 certain property. This is the setting: I need to pick from a list a random element that satisfies a given property. All or none of the elements may have the property. Most of the time, many of the elements will satisfy the property, and the property is a bit expensive to evaluate. Chance of...
0
9311
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
9171
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...
1
9073
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9013
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
7947
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
4719
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2518
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2108
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.