#include "rand01.h"

#define EPS 1.2e-7
#define RNMX (1.0-EPS)
#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836

/* 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 rand1(long *idum)
{
  long k;
  /*  double temp;*/

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