#include <gpk_std.h>
#include <stdlib.h>
#include <mylib.h>
#include <math.h>

#define NRAND	97

#ifndef RAND_MAX
# define RAND_MAX	32767
#endif

int uniform(int x)
{
 static BOOL initted = FALSE;
 static int v[NRAND];
 static unsigned vv;
 int i;
 unsigned y;

 if(!initted)	{
 	initted = TRUE;
	for(i=0;i<NRAND;i++)
		(void)rand();
	for(i=0;i<NRAND;i++)
		v[i] = rand();
	vv = rand();
	}
 vv = v[ y = (vv^(vv>>8)) % NRAND ];
 v[y] = rand();

 return ((long)vv*x)/((long)RAND_MAX+1);
}

#define NF	1000
#define RANGE	4

double lograndom(void);

double lograndom()
{
 static TBOOL initted[NF];
 static float out[NF];
 static BOOL start = FALSE;
 static DOUBLE recurse;
 int x;

 if(!start)	{start = TRUE;	recurse = log((NF/RANGE+0.5)/NF);	}
 x = uniform(NF);
 if(x < NF/RANGE)
 	return lograndom()+recurse;

 if(!initted[x])	{
 	initted[x] = TRUE;
	out[x] = log((double)(x+0.5)/NF);
	}
 return out[x];
}

