home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11711 < prev    next >
Encoding:
Internet Message Format  |  1992-07-29  |  3.4 KB

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