1 // random_state constructor.
7 #include "cln/random.h"
12 #include "cl_base_config.h"
15 #if defined(unix) || defined(__unix) || defined(_AIX) || defined(sinix) || (defined(_WIN32) && defined(__GNUC__)) || defined(__BEOS__)
17 #include <sys/types.h>
18 #include <unistd.h> // declares getpid()
19 #include <stdlib.h> // declares rand()
21 #if defined(HAVE_GETTIMEOFDAY)
24 #ifdef GETTIMEOFDAY_DOTS
25 extern "C" int gettimeofday (struct timeval * tp, ...);
27 extern "C" int gettimeofday (struct timeval * tp, GETTIMEOFDAY_TZP_T tzp);
30 inline uint32 get_seed (void)
32 var struct timeval tv;
34 return cln::highlow32(tv.tv_sec,tv.tv_usec); // 16+16 zufällige Bits
37 #elif defined(HAVE_FTIME)
39 #include <sys/timeb.h>
41 extern "C" void ftime (struct timeb * tp);
43 extern "C" int ftime (struct timeb * tp);
46 inline uint32 get_seed (void)
50 return (tb.time << 10) | tb.millitm; // 22+10 zufällige Bits
53 #elif defined(HAVE_TIMES_CLOCK)
59 #include <sys/times.h>
60 extern "C" clock_t times (struct tms * buffer);
62 inline uint32 get_seed (void)
64 var struct tms tmsbuf;
65 var uint32 seed_lo = times(&tmsbuf);
66 return seed_lo + tmsbuf.tms_utime + tmsbuf.tms_stime;
75 // Counter, to avoid that two random-states created immediately one after
76 // the other contain the same seed.
77 static uint32 counter = 0;
79 random_state::random_state ()
83 #if defined(unix) || defined(__unix) || defined(_AIX) || defined(sinix) || (defined(__MACH__) && defined(__APPLE__)) || (defined(_WIN32) && defined(__GNUC__)) || defined(__BEOS__)
84 seed_lo = ::get_seed();
85 seed_hi = (rand() // zufällige 31 Bit (bei UNIX_BSD) bzw. 16 Bit (bei UNIX_SYSV)
86 << 8) ^ (uintL)(getpid()); // ca. 8 Bit von der Process ID
87 #elif defined(__atarist)
88 seed_lo = highlow32(GEMDOS_GetDate(),GEMDOS_GetTime()); // 16+16 zufällige Bits
89 seed_hi = XBIOS_Random(); // 24 Bit zufällig vom XBIOS, vorne 8 Nullbits
90 #elif defined(amiga) || defined(AMIGA)
91 seed_lo = get_real_time(); // Uhrzeit
92 seed_hi = FindTask(NULL); // Pointer auf eigene Task
93 #elif defined(__MSDOS__) || defined(__EMX__) || defined(__riscos)
94 // Keine Zufallszahlen, keine PID, nichts Zufälliges da.
95 seed_lo = get_real_time(); // Uhrzeit, 100 Hz
98 #error "Must implement random_state constructor!"
100 seed_hi ^= counter++ << 5;