home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!pmafire!news.dell.com!swrinde!sdd.hp.com!wupost!uwm.edu!rpi!batcomputer!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Newsgroups: comp.programming
- Subject: Re: FULL HOUSE
- Message-ID: <9225802.10175@mulga.cs.mu.OZ.AU>
- Date: 13 Sep 92 16:07:22 GMT
- References: <1992Sep13.144341.24950@wuecl.wustl.edu>
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- Lines: 89
-
- ppc1@cec1.wustl.edu (Peter Pui Tak Chiu) writes:
-
- >hi everyone,
- >
- >i am trying to find the probability of getting a hand of full house from a
- >shuffled deck of 52 cards.
- >
- >theoretically, the probability (i think) is:
- >
- > (13 * 4C2) * (12 * 4C3)
- > ----------------------- = 0.001440576
- > 52C5
- >
- >but i wrote a c program to calculate this probability and the results turn
- >out to be very different.
- [...]
- >does anyone know why???
- >
- >i have included the program i wrote. it is written in C++ and is very crude...
- >please try it and give me some feedback.
- >
- >thanx a lot!
-
- [spelling flame on]
- That should be "thanks", not "thanx"!
- [spelling flame off]
-
- >// To calculate the probability of getting a full house
- >
- >#include <iostream.h>
- >#include <time.h>
- >#include <stdlib.h>
- >#include <math.h>
- >
- >int BAD,hand[5]; // BAD when the hand dealt is illegal
- > // e.g. 5 of the same kind.
- > // hand is an array holding the cards dealt
- >long COUNTER=0, GOOD=0; // COUNTER = # of trials
- > // GOOD = # of full house dealt
-
- Avoid using globals variables without good reason.
-
- >void init(){
- >for(int i=0;i<5;i++)hand[i]=rand()%13; // deal the cards
- >}
-
- Here is the cause of your problem.
- Using the above function to deal the cards will not give the same distribution
- as you would get doing it properly. You need to keep track of which cards
- have already been dealt, since once you have dealt 2 aces, for example,
- the chance of getting a third ace should be reduced to less than 1/13 since
- there are only two more left in the deck.
-
- I'd post code showing exactly how to do this, but I'm begining to think
- that maybe this is a homework assignment, in which case you should probably
- do it yourself ;-) It's not that difficult.
-
- >int fullhouse(){ // determine if fullhouse or illegal
- >int counter[13],flag3=0,flag2=0;
- >BAD=0;
- >for(int i=0;i<13;i++)counter[i]=0;
- >for(i=0;i<5;i++)counter[hand[i]]++;
- >for(i=0;i<13;i++){
- > if(counter[i]==3)flag3++; // detect for 3 of a kind
- > if(counter[i]==2)flag2++; // detect for a pair
- > if(counter[i]>4)BAD=1;} // detect for a bad hand
- >return((flag3==1) && (flag2==1));
- >}
- >
- >main(){
- >srand((unsigned)time(NULL)); // initialize random seed
- >while(1){
- >init(); // deal the cards
- >if(fullhouse())GOOD++; // if a FULLHOUSE is dealt, increment
- > // GOOD
- >if(!BAD)COUNTER++; // if the hand dealt is legal
- > // increment COUNTER
- >if(!fmod(COUNTER,10000))cerr<<COUNTER<<" "<<double(GOOD)/COUNTER<<"\n";
- > // print out the result every 10000 trials in the format:
- > // [ # of trials ] [ probability obtained so far to get full house ]
- >}}
-
- Also, I suggest you get hold of a C++ version of "cb" or "indent".
-
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature virus is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-