home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / mutt / random.mut < prev    next >
Text File  |  1988-09-21  |  863b  |  32 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. (include mod.mut)
  17.  
  18. (INT next)
  19. (defun
  20.   rand
  21.   {
  22.     (INT x)
  23.     (next (+ (* next 1103515245) 12345))
  24.     (x (mod (/ next 65536) 32768))
  25.     (if (< x 0) (+ x 32767) x)
  26.   }
  27.   srand (int seed) { (next seed) }
  28. )
  29.  
  30. ;(next 1)
  31. 
  32.