#define EPS 1.2e-7
#define RNMX (1.0-EPS)
#define IM1 2147483563
#define IM2 2147483399
#define AM1 (1.0/IM1)
#define IMM1 (IM1-1)
#define IA1 40014
#define IA2 40392
#define IQ1 53668
#define IQ2 52774
#define IR1 12211
#define IR2 3791

double rand2(long *idum,*idum2)  /* A great random number generator which */
                         /* unfortunately also takes about twice as long */
                         /* as rand1 to run. Also initialised with a */ 
                         /*pointer to a *negative* interger. */
{
  long k;
  long iy;
  double temp;

  k=(*idum)/IQ1;
  *idum=IA1*(*idum-k*IQ1)-k*IR1; /* Compute idum=(IA1*idum)%IM1 without */
  if (*idum < 0) *idum += IM1;   /* overflowing by Schrage's method */
  k=(*idum2)/IQ1;
  *idum2=IA2*(*idum2-k*IQ2)-k*IR2; /* Compute idum2=(IA2*idum)%IM2 likewise */
  if (*idum2 < 0) *idum2 += IM2;
  iy=(*idum)-*idum2;                /* iy the difference of idum and idum2 */
  if (iy < 1) iy += IMM1;           /* Ensure output in the correct range */
  if ((temp=AM1*iy)>RNMX) return RNMX; /* Users don't expect endpoint values */
  else return temp;
}
