home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:11711 comp.sys.amiga.programmer:11783
- Path: sparky!uunet!dtix!darwin.sura.net!gallux.gallaudet.edu!gallua!cads_cole
- From: CADS_COLE@GALLUA.BITNET (Kevin Cole; Washington DC)
- Newsgroups: comp.lang.c,comp.sys.amiga.programmer
- Subject: Re: Matt Dillon's DICE (Amiga C) and rand() function
- Message-ID: <1992Jul29.120044.1@gallua>
- Date: 29 Jul 92 17:00:44 GMT
- References: <1992Jul29.081247.1@gallua>
- Sender: news@gallux.gallaudet.edu (News manager)
- Organization: Gallaudet Research Institute
- Lines: 70
-
- In article <1992Jul29.081247.1@gallua>, I wrote:
-
- > Here's a very short C program that doesn't do what the documentation claims.
- > According to Matt Dillon's DICE documents, I can limit the range of values
- > returned by rand() if I define the variable RAND_MAX to something other than
- > the default. I've tried various values, but I continue to get these huge
- > (32-bit) numbers. Tell me I'm not missing something obvious.
- >
- > #include <stdio.h>
- > #include <stdlib.h>
- > #define RAND_MAX ((1023))
- > main()
- > {
- > int i;
- > for (;;)
- > {
- > i = rand();
- > printf("Random number %04X (%d)\n",i,i);
- > }
- > return(0);
- > }
-
- Thanks all. The *REAL* answer is "Friends don't let friends program stoned."
- I'm recovering from a broken elbow, and the doc's got me on all sorts of brain-
- deadening chemicals.
-
- First, I must confess that I carelessly used the word variable, when I meant
- constant. In Matt Dillon's documentation, it says that RAND_MAX is defined in
- stdlib.h. Sure enough there was a
-
- #define RAND_MAX ((1 << (sizeof(int)*8) - 1)
-
- or something like that. I foolishly assumed from the doc that changing that
- would change the behavior of rand(). If I hadn't been on pain-killers, I might
- have realized that the program I wrote never actually USED my re-definition,
- and that there was no way for rand() to know about a constant in the main
- program that was never actually passed to it.
-
- Some of you explained that what the documentation was actually suggesting was a
- re-compilation of the library containing rand(), after redefining RAND_MAX. The
- pain-killers have worn off sufficiently enough that I knew I didn't want to do
- that. (But just in case I was still brain-dead, one author pointed out that
- this wasn't exactly a bright idea.)
-
- Two of you offered the correct solution: Use the modulus operator %. (One was
- the guy who explained why I didn't want to recompile rand().)
-
- Anyway, many, many thanks to:
-
- Ben Jackson, Dept Biological Structure-University of Washington
- <BJACKSON@GLIA.BIOSTR.WASHINGTON.EDU>
- Anthony Lapadula, University of New Hampshire - Durham, NH
- <AL@KEPLER.UNH.EDU>
- Daniel Barrett, University of Massachusetts, Amherst
- <BARRETT@GLEEP.CS.UMASS.EDU>
- John C. Chu, UB
- <CHU@ACSU.BUFFALO.EDU>
- Boris
- <BORIS%MSC.CORNELL.EDU@CRNLMSC3.BITNET>
- Chris Torek, Lawrence Berkeley Laboratory, Berkeley
- <TOREK%HORSE.EE.LBL.GOV@LBL.BITNET>
- Nick Ruprecht, Institut fuer Informatik der Universitaet Freiburg
- <RUPRECHT@CORSE.INFORMATIK.UNI-FREIBURG.DE>
- --
-
- ========== U.S. Mail =============================== E-Mail =================
- Kevin Cole <Flatline> | Bitnet: CADS_COLE@GALLUA.BITNET
- Gallaudet Research Institute (GRI) | Internet: KJCOLE@GALLUX.GALLAUDET.EDU
- Washington, D.C. 20002-3625 | UUCP: ...!psuvax!gallua.bitnet!kjcole
- (202) 651-5575 x4592 | The WELL: kjcole
-