home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:11792 comp.sys.amiga.programmer:11831
- Newsgroups: comp.lang.c,comp.sys.amiga.programmer
- Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!yale!mintaka.lcs.mit.edu!bloom-picayune.mit.edu!adam.mit.edu!scs
- From: scs@adam.mit.edu (Steve Summit)
- Subject: Re: Matt Dillon's DICE (Amiga C) and rand() function
- Message-ID: <1992Jul30.214117.27748@athena.mit.edu>
- Sender: news@athena.mit.edu (News system)
- Nntp-Posting-Host: adam.mit.edu
- Organization: none, at the moment
- References: <1992Jul29.081247.1@gallua> <1992Jul29.163704.26489@proxxi.se>
- Date: Thu, 30 Jul 1992 20:09:46 GMT
- Lines: 44
-
- [This is a lightly edited version of an article I posted a few
- minutes ago; readers with especially nimble fingers may have seen
- the previous one before I canceled it.]
-
- In article <1992Jul29.163704.26489@proxxi.se>, elias@proxxi.se (Elias M}rtensson (proxxi)) writes:
- > CADS_COLE@GALLUA.BITNET (Kevin Cole; Washington DC) writes:
- >> 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 have never used DICE but it looks like RAND_MAX is an extern int.
- > Try this instead:
- >
- > extern int RAND_MAX;
- > RAND_MAX = 1023;
-
- No. Kevin Cole was (literally!) on drugs when he wrote the above
- about "defin[ing] the variable RAND_MAX to something other than
- the default." RAND_MAX is a macro, provided by the system (in
- <stdlib.h>, on ANSI systems) which tells you the range of values
- which rand() *will* give you. It is not possible to reprogram
- rand()'s range on-the-fly, nor meaningful to redefine RAND_MAX.
-
- To get integral random numbers with a different range, the
- modulus operator is typically used:
-
- rand() % n
-
- gives random numbers in the range 1..n-1 . (Note that this
- technique is slightly inaccurate, and works well only if RAND_MAX
- is much greater than, or an exact multiple of, n).
-
- One legitimate use of RAND_MAX is generating random floating-
- point numbers; for example,
-
- rand() / (double)(RAND_MAX - 1)
-
- should give randomly distributed numbers over the range 0..1 .
- (This method, too, is potentially imperfect; it will have
- problems if RAND_MAX is not exactly representable as a double
- value).
-
- Steve Summit
- scs@adam.mit.edu
-