home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / rand.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  1KB  |  57 lines

  1. /*  rand(3)
  2.  *
  3.  *  Author: Terrence W. Holm          Nov. 1988
  4.  *
  5.  *
  6.  *  A prime modulus multiplicative linear congruential
  7.  *  generator (PMMLCG), or "Lehmer generator".
  8.  *  Implementation directly derived from the article:
  9.  *
  10.  *    S. K. Park and K. W. Miller
  11.  *    Random Number Generators: Good Ones are Hard to Find
  12.  *    CACM vol 31, #10. Oct. 1988. pp 1192-1201.
  13.  *
  14.  *
  15.  *  Using the following multiplier and modulus, we obtain a
  16.  *  generator which:
  17.  *
  18.  *    1)  Has a full period: 1 to 2^31 - 2.
  19.  *    2)  Is testably "random" (see the article).
  20.  *    3)  Has a known implementation by E. L. Schrage.
  21.  */
  22.  
  23. #include <stddef.h>
  24. #include <stdlib.h>
  25.  
  26. #define  A      16807L    /*  A "good" multiplier      */
  27. #define  M   2147483647L    /*  Modulus: 2^31 - 1      */
  28. #define  Q       127773L    /*  M / A          */
  29. #define  R         2836L    /*  M % A          */
  30.  
  31.  
  32. static long _lseed = 1L;
  33.  
  34.  
  35. void srand( seed )
  36.   unsigned int seed;
  37.  
  38.   {
  39.   _lseed = seed;
  40.   }
  41.  
  42.  
  43. int rand()
  44.  
  45.   {
  46.   _lseed = A * (_lseed % Q) - R * (_lseed / Q);
  47.  
  48.   if ( _lseed < 0 )
  49.     _lseed += M;
  50.  
  51. #ifdef __MSHORT__
  52.   return( (int)(_lseed & 0x7fffL) );    /* how "good" is it now ? */
  53. #else
  54.   return( (int) _lseed );
  55. #endif
  56.   }
  57.