#define A 16807L /* multiplier (7**5) */
#define M 2147483647L /* module (2**1-1) */
static long ln[16] = {0L,
1973272912L, 747177549L, 20464843L, 640830765L, 1098742207L,
78126602L, 84743774L, 831312807L, 124667236L, 1172177002L,
1124933064L, 1223960546L, 1878892440L, 1449793615L, 553303732L};
static int strm = 1; /* index of current stream */
/*-------------------------------------------------------------------*/
/* This implementation is for Intel 8086/8 and 80286/386 CPUs using */
/* C compilers with 16-bit short integers and 32-bit long inegers. */
/*-------------------------------------------------------------------*/
double ranf()
{
short *p,*q,k;
long Hi,Lo;
p=(short *)&ln[strm];
Hi=*(p+1) *A;
*(p+1) = 0;
Lo=ln[strm]*A;
p=(short *)&Lo;
Hi+= *(p+1);
q=(short *)&Hi;
*(p+1) = *q&0X7FFF;
k= *(q+1) << 1;
if(*q&0X7FFF) k++;
Lo-=M;
Lo+=k;
if(Lo<0) Lo+=M;
ln[strm]=Lo;
return (double)Lo*4.656612875E-10;
}
보너스. exponential random variables과 geometric random variables 생성 함수.
double expntl(double ramda)
{
return (-log(1-ranf())/ramda);
}
int geometric(double p)
{
if (p > 1.0 || p <= 0.0)
{
printf("invalid number of parameter p(%lf)\n", p);
return 0;
}
return (int)((log(1.0 - ranf())/log(1.0 - p)) + 1);
}