1 // random_state constructor.
7 #include "cln/random.h"
13 #include <windows.h> // for GetCurrentProcessId()
16 #include "cl_base_config.h"
18 #include <cstdlib> // declares rand()
20 #if defined(unix) || defined(__unix) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(_AIX) || defined(sinix) || (defined(__MACH__) && defined(__APPLE__)) || (defined(__CYGWIN__) && defined(__GNUC__)) || defined(__BEOS__)
22 #include <sys/types.h>
23 #include <unistd.h> // declares getpid()
25 #if defined(HAVE_GETTIMEOFDAY)
28 #ifdef GETTIMEOFDAY_DOTS
29 extern "C" int gettimeofday (struct timeval * tp, ...);
31 extern "C" int gettimeofday (struct timeval * tp, GETTIMEOFDAY_TZP_T tzp);
34 inline uint32 get_seed (void)
36 var struct timeval tv;
38 return cln::highlow32(tv.tv_sec,tv.tv_usec); // 16+16 zufällige Bits
41 #elif defined(HAVE_TIMES_CLOCK)
47 #include <sys/times.h>
48 extern "C" clock_t times (struct tms * buffer);
51 inline uint32 get_seed (void)
53 var struct tms tmsbuf;
54 var uint32 seed_lo = times(&tmsbuf);
55 return seed_lo + tmsbuf.tms_utime + tmsbuf.tms_stime;
63 #include <sys/timeb.h>
66 inline uint32 get_seed (void)
70 return cln::highlow32(timebuf.time, (long)(timebuf.millitm)*1000);
78 // Counter, to avoid that two random-states created immediately one after
79 // the other contain the same seed.
80 static uint32 counter = 0;
82 random_state::random_state ()
86 #if defined(unix) || defined(__unix) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(_AIX) || defined(sinix) || (defined(__MACH__) && defined(__APPLE__)) || (defined(__CYGWIN__) && defined(__GNUC__)) || defined(__BEOS__)
87 seed_lo = ::get_seed();
88 seed_hi = (rand() // zufällige 31 Bit (bei UNIX_BSD) bzw. 16 Bit (bei UNIX_SYSV)
89 << 8) ^ (uintL)(getpid()); // ca. 8 Bit von der Process ID
90 #elif defined(__OpenBSD__)
91 seed_lo = arc4random();
92 seed_hi = arc4random();
94 seed_lo = ::get_seed();
95 seed_hi = (rand() << 8) ^ (uintL)(GetCurrentProcessId());
96 #elif defined(__atarist)
97 seed_lo = highlow32(GEMDOS_GetDate(),GEMDOS_GetTime()); // 16+16 zufällige Bits
98 seed_hi = XBIOS_Random(); // 24 Bit zufällig vom XBIOS, vorne 8 Nullbits
99 #elif defined(amiga) || defined(AMIGA)
100 seed_lo = get_real_time(); // Uhrzeit
101 seed_hi = FindTask(NULL); // Pointer auf eigene Task
102 #elif defined(__MSDOS__) || defined(__EMX__) || defined(__riscos)
103 // Keine Zufallszahlen, keine PID, nichts Zufälliges da.
104 seed_lo = get_real_time(); // Uhrzeit, 100 Hz
105 seed_hi = time(NULL);
107 #error "Must implement random_state constructor!"
109 seed_hi ^= counter++ << 5;