1 /* 2 Some of this comes from the TCL 8.0 unix/tclUnixTime.c:TclpGetClicks 3 */ 4 5 #include <time.h> 6 #include <stdlib.h> 7 #include <unistd.h> 8 #ifdef GLIBC2 9 #include <sys/time.h> 10 #endif 11 12 /* 13 Program to run to get some randomness 14 */ 15 #ifndef USE_CPU 16 #define USE_CPU "/bin/false" 17 #endif 18 19 unsigned long GetClicks(void) { 20 unsigned long now; 21 #ifdef NO_GETTOD 22 struct tms dummy; 23 now = (unsigned long) times(&dummy); 24 #else 25 struct timeval date; 26 struct timezone tz; 27 gettimeofday(&date, &tz); 28 now = date.tv_sec*1000000 + date.tv_usec; 29 #endif /* NO_GETTOD */ 30 return now; 31 } 32 33 /* 34 Max is the highest value you want to be returned 35 returns a number between 0 and max that is virtually really random 36 */ 37 38 unsigned long generate_random_number(unsigned long max) { 39 unsigned long tmp1; 40 unsigned long startt,endt, ourrand; 41 startt=GetClicks(); 42 system(USE_CPU); 43 endt=GetClicks(); 44 srand(endt-startt); 45 tmp1=(getpid()*((endt-startt)+1))^rand(); 46 #ifndef GO32 47 srand48(tmp1); 48 ourrand=lrand48() % (max+1); 49 #else 50 rand(tmp1); 51 ourrand=rand() % (max+1); 52 #endif 53 return (ourrand); 54 } |