home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / programm / 2397 < prev    next >
Encoding:
Text File  |  1992-08-20  |  2.3 KB  |  76 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!newsgate.watson.ibm.com!yktnews!admin!curt
  3. From: curt@watson.ibm.com (Curt McDowell)
  4. Subject: Re: Any simple Random Number generators?
  5. Sender: news@watson.ibm.com (NNTP News Poster)
  6. Message-ID: <1992Aug20.143244.6305@watson.ibm.com>
  7. Date: Thu, 20 Aug 1992 14:32:44 GMT
  8. Lines: 61
  9. Disclaimer: This posting represents the poster's views, not necessarily those of IBM
  10. References: <13AUG199218224504@judy.uh.edu> <1992Aug14.104121.29374@corax.udac.uu.se> <3283@creatures.cs.vt.edu> <1992Aug18.143439@fdr.jsc.nasa.gov>
  11. Nntp-Posting-Host: gorby.watson.ibm.com
  12. Organization: IBM T.J. Watson Research Center
  13. Keywords: random, shift
  14.  
  15. In article <1992Aug18.143439@fdr.jsc.nasa.gov>, shirley@fdr.jsc.nasa.gov (Bill Shirley [CSC]) writes:
  16. > If you just want it to reseed differently
  17. > random(time())  
  18. > will do it
  19. > -- 
  20. > Bill Shirley        |  ``Computer Science is not about computers any 
  21. > shirley@fdr.jsc.nasa.gov|    more than astronomy is about telescopes.''
  22.  
  23. No, that won't quite do it.  Firstly, the call is srandom.  Secondly,
  24. time() requires an argument or it will core dump.  What you wanted to
  25. say was srandom(time(0)).
  26.  
  27. A slightly better way is srandom(time(0) + getpid()), since if your
  28. program runs quick it might be run more than once a second (e.g. from a
  29. shell script).
  30.  
  31. Here are my favorite convenience #defines when my program is not in
  32. a hurry:
  33.  
  34. #define rander() (((unsigned long) random() << 5) ^ \
  35.            (unsigned long) random()       ^ \
  36.           ((unsigned long) random() >> 5))
  37.  
  38. #define srnd()   srandom(time(0) + getpid())
  39. #define rnd(n)   (rander() % (n))
  40.  
  41. Here is my favorite way to generate vaguely random numbers when my
  42. program is in a tremendous hurry (e.g. as for test data).  It's a
  43. pseudo-random shift register of the type detailed in a previous note
  44. from Andy Collins, cycling through every possible bit combination
  45. except 0 before recycling.  The numbers are far from ideally random,
  46. but it's very fast.  You may be able to find a way to make them
  47. appear more random.
  48.  
  49. /*
  50.  * randomize_buffer
  51.  *
  52.  *   Fill a buffer of 'len' unsigned longs with semi-random data.
  53.  */
  54.  
  55. void randomize_buffer(buf, len)
  56.     unsigned long  *buf;
  57.     int        len;
  58. {
  59.     unsigned long    b;
  60.  
  61.     while ((b = rander()) == 0)
  62.         ;
  63.  
  64.     while (len--) {
  65.         *buf++ = b;
  66.                 b = b << 1 | (b >> 30 ^ b >> 27) & 1;
  67.     }
  68. }
  69.  
  70. --
  71. Curt McDowell
  72. IBM T. J. Watson Research Center
  73.