home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / me_cd25.zip / MC2MUTT.ZIP / RANDOM.MUT < prev    next >
Text File  |  1992-11-09  |  876b  |  34 lines

  1. ;; Here's a typical (portable) linear-congruential pseudo-random
  2. ;; number generator, taken from the draft ANSI C standard; rand() returns an
  3. ;; integer uniformly distributed from 0 through 32767 (inclusive), and srand()
  4. ;; is used to initialize a sequence to a particular seed, or to avoid getting
  5. ;; a predictable sequence (by setting a seed based on some system variable
  6. ;; such as process ID or time-of-day).
  7.  
  8. ; static unsigned long int next = 1;
  9. ; int rand(void)
  10. ; {
  11. ;   next = next * 1103515245 + 12345;
  12. ;   return (unsigned int)(next/65536) % 32768;
  13. ; }
  14. ; void srand(unsigned int seed) { next = seed; }
  15.  
  16.  
  17. (include mod.mut)
  18.  
  19. (int next)
  20.  
  21. (defun
  22.   rand
  23.   {
  24.     (int x)
  25.  
  26.     (next (+ (* next 1103515245) 12345))
  27.     (x (mod (/ next 65536) 32768))
  28.     (if (< x 0) (+ x 32767) x)
  29.   }
  30.   srand (int seed) { (next seed) }
  31. ; MAIN { (next 1) }
  32. )
  33.  
  34.