home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16106 < prev    next >
Encoding:
Text File  |  1992-11-08  |  3.7 KB  |  94 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!snorkelwacker.mit.edu!bloom-picayune.mit.edu!news
  3. From: scs@adam.mit.edu (Steve Summit)
  4. Subject: Re: Finding RAND_MAX and SEEK_POS (ANSI C)
  5. Message-ID: <1992Nov6.233844.12882@athena.mit.edu>
  6. Sender: news@athena.mit.edu (News system)
  7. Nntp-Posting-Host: adam.mit.edu
  8. Organization: none, at the moment
  9. References: <6507@news.duke.edu> <1992Oct30.170950.21366@klaava.Helsinki.FI>
  10. Date: Fri, 6 Nov 1992 23:38:44 GMT
  11. Lines: 81
  12.  
  13. In article <6507@news.duke.edu>, Ted A. Campbell (tcamp@acpub.duke.edu) writes:
  14. > At least two users--in very different parts of the world--have reported
  15. > that their Unix-based ANSI C compilers (one of them was using a version
  16. > of gcc) failed to find the constants SEEK_POS (for fsek()) and
  17. > RAND_MAX (for rand()).  
  18. > My documentation suggests that these constants ought to be defined
  19. > in ANSI-compliant systems...
  20.  
  21. Indeed they ought.  (In gcc's case, note that its ANSI compliance
  22. is most complete with respect to its parser; gcc often depends on
  23. local #include files and libraries.  If you're using gcc because
  24. it's ANSI-compliant and your vendor-supplied compiler isn't, the
  25. vendor-supplied header files aren't likely to be ANSI-compliant,
  26. either.)
  27.  
  28. > One user reported that he found SEEK_POS defined in <unistd.h>,
  29. > but I'm not aware of the meaning of this header file. 
  30.  
  31. It's a standard Posix header; I hadn't realized that both X3.159
  32. and 1003.1 defined the SEEK_* values.  (Posix uses them for lseek().)
  33.  
  34. > Perhaps someone can reveal these mysteries to me.  Should I include
  35. > default values for these constants (I wouldn't know what they should
  36. > be), or should I simply report that their compilers are broken?
  37.  
  38. Defaulting them can be tricky.  Read on...
  39.  
  40. In article <1992Oct30.170950.21366@klaava.Helsinki.FI>, Lars Wirzenius
  41. (wirzeniu@klaava.Helsinki.FI) writes:
  42. > You could provide a "port.h" or "config.h" header, and let the installer
  43. > of the program add defines for these macros to that header.  As a guess,
  44. > SEEK_SET would probably be 0 (especially on Unix systems where fseek is
  45. > built on lseek, which uses 0 for this purpose).
  46.  
  47. I frequently do things like
  48.  
  49.     #ifndef SEEK_SET
  50.     #define SEEK_SET 0
  51.     #endif
  52.  
  53. , because there are plenty of systems out there without these
  54. macros defined.  (Many systems still tell you, on their man
  55. pages, to use 0, 1, or 2 as the third argument to lseek or fseek.)
  56.  
  57. I was surprised just now to notice that the ANSI C Standard does
  58. *not* specify the numeric values for these macros (unless there's
  59. something I missed), so old code that does
  60.  
  61.     fseek(fp, 0L, 0);
  62.  
  63. could theoretically break on a system which (perversely, but
  64. apparently legally) chose to define SEEK_SET as 37.
  65.  
  66. > If you are not very
  67. > conserned about having an accurate value for RAND_MAX, the value 32767
  68. > should suffice.
  69.  
  70. I would *not* recommend providing a default value for RAND_MAX.
  71. Any code that is using RAND_MAX at all cares about the value more
  72. than just approximately.  On 16-bit machines, RAND_MAX is usually
  73. 32767; on 32-bit machines, it's often 2147483647.  The recommended
  74. way of generating random numbers in a specific range, namely
  75.  
  76.     rand() / (RAND_MAX / N)
  77.  
  78. is going to break badly if the value of RAND_MAX is wildly
  79. incorrect.  (It's extremely tempting to duck the RAND_MAX problem
  80. entirely by computing rand() % N, but doing so cannot be
  81. recommended; see a nearby article of mine, or the FAQ list, for
  82. more details.)
  83.  
  84. If the code finds itself on a system without RAND_MAX locally
  85. #defined, the person doing the installing will have to be asked
  86. to determine the appropriate value somehow.  (Alternately, as
  87. Lars suggested, you could supply your own random number generator,
  88. but doing so sort of defeats the purpose of having one in the
  89. standard library.)
  90.  
  91.                     Steve Summit
  92.                     scs@adam.mit.edu
  93.