#include "rand21.h"

#define IA 16807       /* Multiplier */
#define IM 2147483647  /* Modulus */
#define IQ 127773      /* (long)(IM/IA) */
#define IR 2836        /* IM mod(IA) */

/* Minimal random number generator 
   with period $2^{31}-1$. Initialise 
   with idum a pointer to a long. DO 
   NOT allow *idum to be zero. */
double rand2(long *idum)
{
  if(*idum <0) *idum-=0x80000000UL;

/* Compute idum=(IA*idum)mod(IM) 
   without overflowing by Schrage's method */
  *idum=IA*(*idum%IQ)-IR*(long)(*idum/IQ);    
  if (*idum <0) *idum += IM;                  
  return *idum*1.0/IM;
}
