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

  1. Path: sparky!uunet!noc.near.net!hri.com!spool.mu.edu!umn.edu!lynx!unmvax!bbx!tantalum!edsr!poe!yenne
  2. From: yenne@austin.eds.com (Britt Yenne)
  3. Newsgroups: comp.programming
  4. Subject: Re: FULL HOUSE (SOLUTION)
  5. Message-ID: <1992Sep14.184939.10440@austin.eds.com>
  6. Date: 14 Sep 92 18:49:39 GMT
  7. References: <1992Sep13.223147.28768@wuecl.wustl.edu>
  8. Sender: yenne@austin.eds.com
  9. Organization: EDS Research, Austin, Tx
  10. Lines: 103
  11.  
  12. In article <1992Sep13.223147.28768@wuecl.wustl.edu> ppc1@cec1.wustl.edu (Peter Pui Tak Chiu) writes:
  13. >
  14. >int getRandom(){
  15. >int n,BAD=1;
  16. >while(BAD){
  17. >n=rand()%52+1;
  18. >for(int i=0;i<5;i++){
  19. >    int FLAG=0;
  20. >    if(n==hand[i])FLAG=1;
  21. >    if(!FLAG)BAD=0;
  22. >    }}
  23. >return(n);
  24. >}
  25.  
  26. [possibly-related tangent mode on]
  27.  
  28. One thing I should mention is that, regardless of whether or not your
  29. programs are correct, you are all using the rand() function.  Rand() is
  30. a notoriously bad random number generator.  For instance, it always
  31. alternates between odd and even numbers.  Try choosing a lot of "random"
  32. screen X and Y coordinates and place characters there... you'll be
  33. shocked at the obvious pattern.
  34.  
  35. Try using srandom() and random().
  36.  
  37. [possibly-related tangent mode off]
  38.  
  39. The big problem with simulating real-world probabilities is accurately
  40. putting the problem to code.  Simulate a deck.  Simulate a shuffle.
  41. In computer science classes, one is told to avoid such algorithms
  42. which are wasteful and could be done "better."
  43.  
  44. Here is a program I wrote.  It's complete brute force and I'm not proud
  45. of it, but it gives these results:
  46.  
  47. Got 146 full houses out of 100000 deals --> 0.001460
  48. Got 140 full houses out of 100000 deals --> 0.001400
  49. Got 145 full houses out of 100000 deals --> 0.001450
  50.  
  51. ciao,
  52. -britt
  53.  
  54. --------- cut here -----------
  55.  
  56. #include <stdio.h>
  57.  
  58. int
  59. main()
  60. {
  61.     register int i, n, s, pass;
  62.     int card[52], save[2], count[2];
  63.     int fh = 0;
  64.  
  65.     /* build a deck... suit is irrelevant */
  66.  
  67.     srandom(getpid());
  68.     for(i = 0; i < 52; i++)
  69.     card[i] = i % 13;
  70.  
  71.     /* now the test cases */
  72.  
  73.     for(pass = 0; pass < 100000; pass++)
  74.     {
  75.     /* shuffle the first five cards of the deck -- don't be clever,
  76.      * just do it the hard way
  77.      */
  78.  
  79.     for(i = 0; i < 5; i++)
  80.     {
  81.         n = random() % 52;
  82.         s = card[n];
  83.         card[n] = card[i];
  84.         card[i] = s;
  85.     }
  86.  
  87.     /* examine the first 5 cards */
  88.  
  89.     s = 0;
  90.     for(i = 0; i < 5; i++)
  91.     {
  92.         if(s >= 1 && card[i] == save[0])
  93.         count[0]++;
  94.         else if(s >= 2 && card[i] == save[1])
  95.         count[1]++;
  96.         else if(s >= 2)    /* more than two types now */
  97.         break;
  98.         else
  99.         {
  100.         save[s] = card[i];
  101.         count[s] = 1;
  102.         s++;
  103.         }
  104.     }
  105.  
  106.     /* is it a full house? */
  107.  
  108.     if((count[0] == 2 && count[1] == 3) || (count[1] == 2 && count[0] == 3))
  109.         fh++;
  110.     }
  111.     printf("Got %d full houses out of %d deals --> %f\n", fh, pass,
  112.     (double)((double)fh / (double)pass));
  113.     return 0;
  114. }
  115.