/* tools.c: a collection for routines to provide timing and random numbers. * The timer returns execution time in fractional * seconds. Integer and floating point random number generators are * provided. Routines are also included to initialize the timer and random * number generators. * * The routine which initializes the random number generator takes a * single integer argument; if this argument <= zero, the seed * is initialized from the CPU clock; a positive argument is used * directly as the seed. * * G. S. Stiles; 25 Aug. 1993. * Modified from timers.c to include the improved random number * generator from Park & Miller, CACM Oct. 1988. (Actually their later * improved version, as available via FTP...see CACM July 1993, pp. 105- * 110. The period of the generator is 2 147 483 646. The package will * provide 256 different streams. The default is stream 0; others may be * selected with the function Select_Stream(stream_no). The seeds for the * streams are separated by 8 367 782 steps. See rngs.c for details.) * * The largest random integer which can be generated is 2 147 483 646. */ /* for the use mit GNU C-Compiler*/ #define GNUC #include #include #include "parrandom.h" #define MIN(x,y) ( (x < y) ? x : y) #define MAX(x,y) ((x > y) ? x : y) void moments(int samples, float *sumx, float *sumy) { if (samples > 0){ *sumx = *sumx/samples; if (samples > 1){ *sumy = (*sumy - samples*(*sumx)*(*sumx))/(samples - 1); if (*sumy > 0.0) *sumy = sqrt(*sumy); else *sumy = 0.0; } else *sumy = 0.0; } else { *sumx = 0.0; *sumy = 0.0; } } void Dmoments(int samples, double *sumx, double *sumy) { if (samples > 0){ *sumx = *sumx/samples; if (samples > 1){ *sumy = (*sumy - samples*(*sumx)*(*sumx))/(samples - 1); if (*sumy > 0.0) *sumy = sqrt(*sumy); else *sumy = 0.0; } else *sumy = 0.0; } else { *sumx = 0.0; *sumy = 0.0; } } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * GNU C versions. * */ #include #include #include /*#include */ /* Static variables: */ long INITIAL_TIME; /* * init_time: initialize the execution timer. * */ void init_time() { struct tms *timebuf; timebuf = (struct tms *)malloc(sizeof(struct tms)); times(timebuf); INITIAL_TIME = timebuf->tms_utime; } /* * get_time: returns time in float seconds elapsed since call to init_time; * */ float get_time() { float temp; long this_time; struct tms *timebuf; timebuf = (struct tms *)malloc(sizeof(struct tms)); times(timebuf); this_time = timebuf->tms_utime; temp = ((float)(this_time - INITIAL_TIME))/60.0; return(temp); } void get_datime(char *str_out) { /* * Date and time stamper; stolen from Turbo C manual. * Dyke Stiles; 3/31/91 * This version places a string containing the time and date * in str_out. GSS; 2/15/92. The destination string in the calling * program (str_now) should be declared with a length of [25]. * */ time_t secs_now; char *str_now; /* Get the time in seconds: */ time(&secs_now); /* Convert to string: */ str_now = ctime(&secs_now); /*Get rid of the carriage return: */ str_now[24] = '\0'; strncpy(str_out,str_now,25); } /* * init_rand initializes the random number generator. */ void init_rand(int new_seed) { int i_temp; if (new_seed <= 0){ i_temp = time(0); Randomize(i_temp); } else{ Randomize(new_seed); } } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /* * Common routines * */ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * rand_int returns a random integer in the range [0,limit). * Note that Select_Stream must first be called to set the correct * stream. * */ int rand_int(int limit) { long int i_temp; float f_temp; /* Modified to work with rngs.c, which does not supply an explicit * integer random number. */ /* Call for rngs float first so that new integer seed is generated: */ f_temp = Random(); Get_Seed(&i_temp); i_temp = i_temp % limit; return(i_temp); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * rand_float returns a random floating point number in the range [0,1); * */ float rand_float() { return((float) Random()); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * rand16_float returns a random floating point number in the range [0,1); * The random number is based on 16 bit integers. */ float rand16_float() { int temp1; temp1 = rand_int(32767); return((float) temp1/((float)32767)); } /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * rand8_float returns a random floating point number in the range [0,1); * The random number is based on 16 bit integers. */ float rand8_float() { int temp1; temp1 = rand_int(255); return((float) temp1/((float)255)); } /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * * rand64_float returns a random floating point number generated from * a (equivalent) 64 bit integer. Sort of. float rand64_float() { int offset; double temp1; long int s; Get_Seed(&s); temp1= Random(); offset = rand_int(20) + 1; Select_Stream(s + offset); temp1 = temp1 + Random()/((double)MAXINT); Select_Stream(s - offset); return((float) temp1 ); } */ /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * * rand64_double() returns a random double precision floating point * number generated from essentially a 64-bit integer. double rand64_double() { return(Random() + Random()/((double) MAXINT)); } */ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * rand_double returns a random double floating point number in [0,1); * */ double rand_double() { return(Random()); }