home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!noc.near.net!hri.com!spool.mu.edu!umn.edu!lynx!unmvax!bbx!tantalum!edsr!poe!yenne
- From: yenne@austin.eds.com (Britt Yenne)
- Newsgroups: comp.programming
- Subject: Re: FULL HOUSE (SOLUTION)
- Message-ID: <1992Sep14.184939.10440@austin.eds.com>
- Date: 14 Sep 92 18:49:39 GMT
- References: <1992Sep13.223147.28768@wuecl.wustl.edu>
- Sender: yenne@austin.eds.com
- Organization: EDS Research, Austin, Tx
- Lines: 103
-
- In article <1992Sep13.223147.28768@wuecl.wustl.edu> ppc1@cec1.wustl.edu (Peter Pui Tak Chiu) writes:
- >
- >int getRandom(){
- >int n,BAD=1;
- >while(BAD){
- >n=rand()%52+1;
- >for(int i=0;i<5;i++){
- > int FLAG=0;
- > if(n==hand[i])FLAG=1;
- > if(!FLAG)BAD=0;
- > }}
- >return(n);
- >}
-
- [possibly-related tangent mode on]
-
- One thing I should mention is that, regardless of whether or not your
- programs are correct, you are all using the rand() function. Rand() is
- a notoriously bad random number generator. For instance, it always
- alternates between odd and even numbers. Try choosing a lot of "random"
- screen X and Y coordinates and place characters there... you'll be
- shocked at the obvious pattern.
-
- Try using srandom() and random().
-
- [possibly-related tangent mode off]
-
- The big problem with simulating real-world probabilities is accurately
- putting the problem to code. Simulate a deck. Simulate a shuffle.
- In computer science classes, one is told to avoid such algorithms
- which are wasteful and could be done "better."
-
- Here is a program I wrote. It's complete brute force and I'm not proud
- of it, but it gives these results:
-
- Got 146 full houses out of 100000 deals --> 0.001460
- Got 140 full houses out of 100000 deals --> 0.001400
- Got 145 full houses out of 100000 deals --> 0.001450
-
- ciao,
- -britt
-
- --------- cut here -----------
-
- #include <stdio.h>
-
- int
- main()
- {
- register int i, n, s, pass;
- int card[52], save[2], count[2];
- int fh = 0;
-
- /* build a deck... suit is irrelevant */
-
- srandom(getpid());
- for(i = 0; i < 52; i++)
- card[i] = i % 13;
-
- /* now the test cases */
-
- for(pass = 0; pass < 100000; pass++)
- {
- /* shuffle the first five cards of the deck -- don't be clever,
- * just do it the hard way
- */
-
- for(i = 0; i < 5; i++)
- {
- n = random() % 52;
- s = card[n];
- card[n] = card[i];
- card[i] = s;
- }
-
- /* examine the first 5 cards */
-
- s = 0;
- for(i = 0; i < 5; i++)
- {
- if(s >= 1 && card[i] == save[0])
- count[0]++;
- else if(s >= 2 && card[i] == save[1])
- count[1]++;
- else if(s >= 2) /* more than two types now */
- break;
- else
- {
- save[s] = card[i];
- count[s] = 1;
- s++;
- }
- }
-
- /* is it a full house? */
-
- if((count[0] == 2 && count[1] == 3) || (count[1] == 2 && count[0] == 3))
- fh++;
- }
- printf("Got %d full houses out of %d deals --> %f\n", fh, pass,
- (double)((double)fh / (double)pass));
- return 0;
- }
-