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

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!ames!olivea!sgigate!odin!fido!babar.asd.sgi.com!mtj
  2. From: mtj@babar.asd.sgi.com (Michael Jones)
  3. Newsgroups: comp.programming
  4. Subject: Re: FULL HOUSE (REVISED)
  5. Message-ID: <prn5c10@fido.asd.sgi.com>
  6. Date: 14 Sep 92 20:15:20 GMT
  7. References: <1992Sep13.223147.28768@wuecl.wustl.edu> <LOWRY.92Sep14133846@rotor.watson.ibm.com>
  8. Sender: news@fido.asd.sgi.com (Usenet News Admin)
  9. Organization: Silicon Graphics, Inc.
  10. Lines: 113
  11.  
  12. Well, I rewrote it rather than debugging it. I've seen lots of
  13. ways to simulate "random cards" that just don't work. Whenever
  14. a Monte-Carlo simulation (literal, this time) has problems it
  15. is wise to inspect the source and use of random numbers.
  16.  
  17. Advcie:
  18.  
  19.  1. Calling rand() is risky, stick with random().
  20.  
  21.  2. There is a much better way to shuffle the deck (see below).
  22.  
  23. Code (which may have faults of its own, no doubt):
  24.  
  25. -------8<------- Cut Here -----8<----- Cut Here -----8K-----
  26.  
  27. #include <stdio.h>
  28.  
  29. void make (int *deck)
  30. {
  31. int card;
  32.  
  33. for (card = 0; card < 52; card++)
  34.     deck[card] = card % 13;
  35. }
  36.  
  37. void shuffle (int *deck)
  38. {
  39. int card;
  40. int swap;
  41. int temp;
  42.  
  43. for (card = 0; card < 52; card++)
  44.     {
  45.     swap = random() % 52;
  46.  
  47.     temp = deck[card];
  48.            deck[card] = deck[swap];
  49.                         deck[swap] = temp;
  50.     }
  51. }
  52.  
  53. int test (int *hand)
  54. {
  55. int card;
  56. int count[13];
  57. int sets[6];
  58.  
  59. for (card = 0; card < 13; card++)
  60.     count[card] = 0;
  61.  
  62. for (card = 0; card <  5; card++)
  63.     count[hand[card]]++;
  64.  
  65. for (card = 0; card <  5; card++)
  66.     sets[card] = 0;
  67.  
  68. for (card = 0; card < 13; card++)
  69.     sets[count[card]]++;
  70.  
  71. return (sets[2] == 1 && sets[3] == 1) ? 1 : 0;
  72. }
  73.  
  74. int main (int argc, char *argv[])
  75. {
  76. int card;
  77. int hands = 0;
  78. int found = 0;
  79. int deck[52];
  80.  
  81. make(deck);
  82.  
  83. while (1)
  84.     {
  85.     shuffle(deck);
  86.  
  87.     for (card = 0; card < 52 - 5; card += 5, hands++)
  88.         found += test(&deck[card]);
  89.  
  90.     if ((hands % 1000000) == 0)
  91.         {
  92.         printf("f=%5d h=%10d f=%16.14f%\n", found, hands, found/(double)hands);
  93.         fflush(stdout);
  94.         }
  95.     }
  96. }
  97.  
  98. -------8<------- Cut Here -----8<----- Cut Here -----8K-----
  99.  
  100. Results:
  101.  
  102. babar!mtj% cc -o right right.c
  103. babar!mtj% right
  104. f= 1403 h=   1000000 f=0.00140300000000
  105. f= 2879 h=   2000000 f=0.00143950000000
  106. f= 4312 h=   3000000 f=0.00143733333333
  107. f= 5804 h=   4000000 f=0.00145100000000
  108. f= 7218 h=   5000000 f=0.00144360000000
  109. f= 8661 h=   6000000 f=0.00144350000000
  110. f=10074 h=   7000000 f=0.00143914285714
  111.  :
  112. f=626140 h= 435000000 f=0.00143940229885
  113. f=627612 h= 436000000 f=0.00143947706422
  114. f=629051 h= 437000000 f=0.00143947597254
  115. ^C
  116. -->> 95% 3:21:14 11542.0u 38.1s 48kmem <<--
  117.  
  118. Timing on my workstation, an SGI 4D420/VGX (2 40Mhz R3000's, 32MB ram). 
  119.  
  120. By the way ... how did you derive the analytic result?
  121.  
  122. -- Michael Jones    mtj@sgi.com  415.390.1455  M/S 7L-590
  123.             Silicon Graphics, Advanced Graphics Division
  124.             2011 N. Shoreline Blvd., Mtn. View, CA 94039-7311
  125.