/*****************************************************************************/ /*random.c Notes on usage : 1. In your main program add, extern float rand1(void); extern float slete(float , float ); 2. From the main program call " slete(std, mean) " where std, mean are the standard deviation, mean of the gaussian pdf respectively. 3. The return value of slete(std, mean) is a gaussian random number. /*****************************************************************************/ #include #include #include #define PI 3.141592654 #define SEED1 29 #define SEED2 3191 #define SEED3 1987 static struct RandomNum { int ix, iy, iz; }; static struct RandomNum random = { SEED1,SEED2,SEED3},*randptr = &random; float rand1(void) { int ixx, iyy, izz, itemp; float temp, uniform; ixx = randptr->ix/177; randptr->ix = 171*(randptr->ix % 177) - 2*ixx; if ( randptr->ix < 0) { randptr->ix += 30269; } iyy = randptr->iy/176; randptr->iy = 172*(randptr->iy % 176) - 35*iyy; if ( randptr->iy < 0) { randptr->iy += 30307; } izz = randptr->iz/178; randptr->iz = 170*(randptr->iz % 178) - 63*izz; if ( randptr->iz < 0) { randptr->iz += 30323; } temp = (float)randptr->ix/30269.0 + (float)randptr->iy/30307.0 + (float)randptr->iz/30323.0; itemp = (int)temp; uniform = temp - itemp; return(uniform); } /******************************************************************************/ /* function slete() generates a gaussian random process by invoking rand1() and manipulating the sample returned by it. */ /******************************************************************************/ float slete(float std, float mean) { float rand1(void); float gaussian, temp1, temp2; temp1 = cos(2*PI*rand1()); temp2 = sqrt( -2.0 * log(rand1())); gaussian = mean + std * temp1 * temp2; return(gaussian); } /***************************************************************************/