/****************************************/ /* void InitRandom(); */ /* integer Random1To(integer N); */ /* integer Random01(real threshold); */ /****************************************/ /*********************************************************/ /* Creates seeds by using seconds and milliseconds */ /* Uses only 7 bits of sec == approx 2 minutes */ /* Uses higher order bits of system function random() */ /*********************************************************/ #include #define MY_RAND_MAX 2147483647 void InitRandom() { struct timeval tv; struct timezone tzp; long sec, msec; gettimeofday(&tv,&tzp); sec = (long) tv.tv_sec; msec = (long) tv.tv_usec; srandom((unsigned int)(((sec & 0177 ) * 1000000) + msec)); } integer Random1To(integer n) { return (1+(integer)(((double)n)*random()/(1.0+MY_RAND_MAX))); } integer Random01(real threshold) { if ( random()<=(long)(threshold*MY_RAND_MAX) ) return 1; else return 0; }