home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!ames!olivea!sgigate!odin!fido!babar.asd.sgi.com!mtj
- From: mtj@babar.asd.sgi.com (Michael Jones)
- Newsgroups: comp.programming
- Subject: Re: FULL HOUSE (REVISED)
- Message-ID: <prn5c10@fido.asd.sgi.com>
- Date: 14 Sep 92 20:15:20 GMT
- References: <1992Sep13.223147.28768@wuecl.wustl.edu> <LOWRY.92Sep14133846@rotor.watson.ibm.com>
- Sender: news@fido.asd.sgi.com (Usenet News Admin)
- Organization: Silicon Graphics, Inc.
- Lines: 113
-
- Well, I rewrote it rather than debugging it. I've seen lots of
- ways to simulate "random cards" that just don't work. Whenever
- a Monte-Carlo simulation (literal, this time) has problems it
- is wise to inspect the source and use of random numbers.
-
- Advcie:
-
- 1. Calling rand() is risky, stick with random().
-
- 2. There is a much better way to shuffle the deck (see below).
-
- Code (which may have faults of its own, no doubt):
-
- -------8<------- Cut Here -----8<----- Cut Here -----8K-----
-
- #include <stdio.h>
-
- void make (int *deck)
- {
- int card;
-
- for (card = 0; card < 52; card++)
- deck[card] = card % 13;
- }
-
- void shuffle (int *deck)
- {
- int card;
- int swap;
- int temp;
-
- for (card = 0; card < 52; card++)
- {
- swap = random() % 52;
-
- temp = deck[card];
- deck[card] = deck[swap];
- deck[swap] = temp;
- }
- }
-
- int test (int *hand)
- {
- int card;
- int count[13];
- int sets[6];
-
- for (card = 0; card < 13; card++)
- count[card] = 0;
-
- for (card = 0; card < 5; card++)
- count[hand[card]]++;
-
- for (card = 0; card < 5; card++)
- sets[card] = 0;
-
- for (card = 0; card < 13; card++)
- sets[count[card]]++;
-
- return (sets[2] == 1 && sets[3] == 1) ? 1 : 0;
- }
-
- int main (int argc, char *argv[])
- {
- int card;
- int hands = 0;
- int found = 0;
- int deck[52];
-
- make(deck);
-
- while (1)
- {
- shuffle(deck);
-
- for (card = 0; card < 52 - 5; card += 5, hands++)
- found += test(&deck[card]);
-
- if ((hands % 1000000) == 0)
- {
- printf("f=%5d h=%10d f=%16.14f%\n", found, hands, found/(double)hands);
- fflush(stdout);
- }
- }
- }
-
- -------8<------- Cut Here -----8<----- Cut Here -----8K-----
-
- Results:
-
- babar!mtj% cc -o right right.c
- babar!mtj% right
- f= 1403 h= 1000000 f=0.00140300000000
- f= 2879 h= 2000000 f=0.00143950000000
- f= 4312 h= 3000000 f=0.00143733333333
- f= 5804 h= 4000000 f=0.00145100000000
- f= 7218 h= 5000000 f=0.00144360000000
- f= 8661 h= 6000000 f=0.00144350000000
- f=10074 h= 7000000 f=0.00143914285714
- :
- f=626140 h= 435000000 f=0.00143940229885
- f=627612 h= 436000000 f=0.00143947706422
- f=629051 h= 437000000 f=0.00143947597254
- ^C
- -->> 95% 3:21:14 11542.0u 38.1s 48kmem <<--
-
- Timing on my workstation, an SGI 4D420/VGX (2 40Mhz R3000's, 32MB ram).
-
- By the way ... how did you derive the analytic result?
-
- -- Michael Jones mtj@sgi.com 415.390.1455 M/S 7L-590
- Silicon Graphics, Advanced Graphics Division
- 2011 N. Shoreline Blvd., Mtn. View, CA 94039-7311
-