/* Linear congruential generator of Lewis and Payne,    */
/* using the Schrage modulus trick. Taken from          */
/* Monte Carlo Methods in Statistical Physics,          */
/* M.E.J. Newman and G.T. Barkema,                      */
/* Clarendon Press, Oxford (1999), pp 451.              */
/*                                                      */    
/* Lewis, T.G. and Payne, W.H. 1973 Journal ACM 20, 456 */
/* Schrage, L. 1979 ACM Trans. Math. Software 5, 132    */


extern long seed; /* non-zero integer */

#define a 16807
#define m 2147483647
#define q 127773
#define r 2836
#define conv (1.0/(m-1))

double lcg()
{
  long l;

  l = seed/q;
  seed = a*(seed-q*l) - r*l;
  if (seed<0) seed += m;

  return conv*(seed-1);
}
