home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / programm / 2597 < prev    next >
Encoding:
Text File  |  1992-09-13  |  3.3 KB  |  101 lines

  1. 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
  2. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  3. Newsgroups: comp.programming
  4. Subject: Re: FULL HOUSE
  5. Message-ID: <9225802.10175@mulga.cs.mu.OZ.AU>
  6. Date: 13 Sep 92 16:07:22 GMT
  7. References: <1992Sep13.144341.24950@wuecl.wustl.edu>
  8. Sender: news@cs.mu.OZ.AU
  9. Organization: Computer Science, University of Melbourne, Australia
  10. Lines: 89
  11.  
  12. ppc1@cec1.wustl.edu (Peter Pui Tak Chiu) writes:
  13.  
  14. >hi everyone,
  15. >
  16. >i am trying to find the probability of getting a hand of full house from a
  17. >shuffled deck of 52 cards.
  18. >
  19. >theoretically, the probability (i think) is:
  20. >
  21. >        (13 * 4C2) * (12 * 4C3)
  22. >        -----------------------  =  0.001440576
  23. >                 52C5
  24. >
  25. >but i wrote a c program to calculate this probability and the results turn
  26. >out to be very different.
  27. [...]
  28. >does anyone know why???
  29. >
  30. >i have included the program i wrote.  it is written in C++ and is very crude...
  31. >please try it and give me some feedback.
  32. >
  33. >thanx a lot!
  34.  
  35. [spelling flame on]
  36. That should be "thanks", not "thanx"!
  37. [spelling flame off]
  38.  
  39. >// To calculate the probability of getting a full house
  40. >
  41. >#include <iostream.h>
  42. >#include <time.h>
  43. >#include <stdlib.h>
  44. >#include <math.h>
  45. >
  46. >int BAD,hand[5];        // BAD when the hand dealt is illegal
  47. >                // e.g. 5 of the same kind.
  48. >                // hand is an array holding the cards dealt
  49. >long COUNTER=0, GOOD=0;        // COUNTER = # of trials
  50. >                // GOOD = # of full house dealt
  51.  
  52. Avoid using globals variables without good reason.
  53.  
  54. >void init(){
  55. >for(int i=0;i<5;i++)hand[i]=rand()%13;    // deal the cards
  56. >}
  57.  
  58. Here is the cause of your problem.
  59. Using the above function to deal the cards will not give the same distribution
  60. as you would get doing it properly. You need to keep track of which cards
  61. have already been dealt, since once you have dealt 2 aces, for example,
  62. the chance of getting a third ace should be reduced to less than 1/13 since
  63. there are only two more left in the deck.
  64.  
  65. I'd post code showing exactly how to do this, but I'm begining to think
  66. that maybe this is a homework assignment, in which case you should probably
  67. do it yourself ;-) It's not that difficult.
  68.  
  69. >int fullhouse(){            // determine if fullhouse or illegal
  70. >int counter[13],flag3=0,flag2=0;
  71. >BAD=0;
  72. >for(int i=0;i<13;i++)counter[i]=0;
  73. >for(i=0;i<5;i++)counter[hand[i]]++;
  74. >for(i=0;i<13;i++){
  75. >    if(counter[i]==3)flag3++;    // detect for 3 of a kind
  76. >    if(counter[i]==2)flag2++;    // detect for a pair
  77. >    if(counter[i]>4)BAD=1;}        // detect for a bad hand
  78. >return((flag3==1) && (flag2==1));
  79. >}
  80. >
  81. >main(){
  82. >srand((unsigned)time(NULL));        // initialize random seed
  83. >while(1){
  84. >init();                    // deal the cards
  85. >if(fullhouse())GOOD++;            // if a FULLHOUSE is dealt, increment
  86. >                    // GOOD
  87. >if(!BAD)COUNTER++;            // if the hand dealt is legal
  88. >                    // increment COUNTER
  89. >if(!fmod(COUNTER,10000))cerr<<COUNTER<<" "<<double(GOOD)/COUNTER<<"\n";
  90. >    // print out the result every 10000 trials in the format:
  91. >    // [ # of trials ] [ probability obtained so far to get full house ]
  92. >}}
  93.  
  94. Also, I suggest you get hold of a C++ version of "cb" or "indent".
  95.  
  96. -- 
  97. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  98. This .signature virus is a self-referential statement that is true - but 
  99. you will only be able to consistently believe it if you copy it to your own
  100. .signature file!
  101.