/************************************************************************ * * * Purpose: * * randX is a library for random number generation. * * The integer randXseed should be initialized to an arbitrary * * integer prior to the first call to the desired function. The calling * * program should not alter the value of randXseed between subsequent * * calls. * * * * Author: * * Frederico R. B. Cruz * * Departamento de Estatistica * * Universidade Federal de Minas Gerais * * Brazil * * E-mail: fcruz@est.ufmg.br * * * * Version: * * 1.00 * * * * Date: * * March/2002 * * * ************************************************************************/ #ifndef RANDX_C #define RANDX_C #include #define RANDX_PI 3.14159265358979323846 #define RANDX_EE 2.71828182845904523536 /* uniform [0,1] */ float rUnif(int *randXseed); float rUnif2(unsigned long *randXseed1, unsigned long *randXseed2); /* normal(mu=0,sd=1) */ float rNorm(int *randXseed); /* exponetial(lambda) */ float rExpo(int *randXseed, float lambda); /* gamma(alpha) */ double rGamma(int *randXseed, double alpha); /* beta(alpha,beta) */ double rBeta(int *randXseed, double alpha, double beta); /************************************************************************ * * * The rUnif function is a uniform random number generator based * * on theory and suggestions given in Forsythe, G.E., Malcolm, M.A., & * * Moter, C.B. Computer Methods For Mathematical Computations, * * Prentice-Hall, 1977. * * The integer randXseed should be initialized to an arbitrary * * integer prior to the first call to rUnif. The calling program should * * not alter the value of randXseed between subsequent calls to rUnif. * * Values of rUnif will be returned in the interval (0,1). * * * ************************************************************************/ float rUnif(int *randXseed) { /* initialized data */ static int m2 = 0; static int two = 2; /* local variables */ static int m; static float s; static float halfm; static int a, c, mc; /* if first entry then ... */ if (m2 == 0) { /* compute machine integer word length */ m = 1; do { m2 = m; m = two * m2; } while( m > m2 ); halfm = (float) m2; /* compute multiplier and increment for linear */ /* congruential method */ a = ( (int) ( halfm * atan(1.) / 8.) << 3 ) + 5; c = ( (int) ( halfm * ( 0.5 - sqrt(3.) / 6.)) << 1 ) + 1; mc = m2 - c + m2; /* s is the scale factor for converting to floating point */ s = 0.5 / halfm; } /* compute next random number */ *randXseed *= a; /* the following statement is for computers which do not */ /* allow integer overflow on addition */ if (*randXseed > mc) *randXseed = *randXseed - m2 - m2; *randXseed += c; /* the following statement is for computers where the word */ /* length for addition is greater than for multiplication */ if (*randXseed / 2 > m2) *randXseed = *randXseed - m2 - m2; /* the following statement is for computers where integer */ /* overflow affects the sign bit */ if (*randXseed < 0) *randXseed = *randXseed + m2 + m2; return ( (float) (*randXseed) * s ); } /************************************************************************ * * * George Marsaglia's uniform random number generator * * The integers randXseed1 and randXseed2 should be initialized * * to an arbitrary integer prior to the first call to rUnif2. The * * calling program should not alter these values between subsequent * * calls to rUnif. * * * ************************************************************************/ float rUnif2(unsigned long *randXseed1, unsigned long *randXseed2) { static unsigned long s1new, s2new; s1new = ((*randXseed1=36969*(*randXseed1&65535)+(*randXseed1>>16))<<16); s2new = ((*randXseed2=18000*(*randXseed2&65535)+(*randXseed2>>16))&65535); return ((s1new+s2new)*2.32830643708E-10); } /************************************************************************ #include #include "uni.c" int main() { int replic = 1000; int randXseed = 123456; unsigned long randXseed1=362436069, randXseed2=521288629; int i; for (i=0; i= 0) { if (rUnif(randXseed) > 0.5) return -y1; else return y1; } } } /************************************************************************ #include int main() { int replic = 100000; float mu = 0; float sigma = 1; int seed = 123456; float numb; float sum, sum2; int i; sum = 0.0; sum2 = 0.0; for (i=0; i int main() { int replic = 10000; float lambda = 2; int seed = 123456; float numb; float sum = 0.0; float sum2 = 0.0; int i; int cont; for (i=0; i1./aa) { x = -log(aa*(1.-r1)/alpha); if (r22.5) r1=r2+c5*(1.-1.86*r1); } while(r1<=0 || r1 >= 1); w=c2*r2/r1; if (c3*r1+w+1/w <= c4) return c1*w; if (c3*log(r1)-log(w)+w<1) return c1*w; } while (1); } } /************************************************************************ #include int main() { int replic = 10000; int seed = 123456; int i; double alpha = 1.5; for (i=0; i int main() { int replic = 10000; int seed = 123456; int i; double alpha = 2.0; double beta = 3.0; for (i=0; i