1 // random_state constructor.
5 #include <windows.h> // For GetCurrentProcessId(), must be included first, sorry.
12 #include "cln/random.h"
17 #include "cl_base_config.h"
19 #include <cstdlib> // declares rand()
21 #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__)
23 #include <sys/types.h>
24 #include <unistd.h> // declares getpid()
26 #if defined(HAVE_GETTIMEOFDAY)
29 #ifdef GETTIMEOFDAY_DOTS
30 extern "C" int gettimeofday (struct timeval * tp, ...);
32 extern "C" int gettimeofday (struct timeval * tp, GETTIMEOFDAY_TZP_T tzp);
36 inline uint32 get_seed (void)
38 var struct timeval tv;
40 return highlow32(tv.tv_sec,tv.tv_usec); // 16+16 zufällige Bits
44 #elif defined(HAVE_TIMES_CLOCK)
50 #include <sys/times.h>
51 extern "C" clock_t times (struct tms * buffer);
54 inline uint32 get_seed (void)
56 var struct tms tmsbuf;
57 var uint32 seed_lo = times(&tmsbuf);
58 return seed_lo + tmsbuf.tms_utime + tmsbuf.tms_stime;
66 #include <sys/timeb.h>
69 inline uint32 get_seed (void)
73 return highlow32(timebuf.time, (long)(timebuf.millitm)*1000);
81 // Counter, to avoid that two random-states created immediately one after
82 // the other contain the same seed.
83 static uint32 counter = 0;
85 random_state::random_state ()
89 #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__)
91 seed_hi = (rand() // zufällige 31 Bit (bei UNIX_BSD) bzw. 16 Bit (bei UNIX_SYSV)
92 << 8) ^ (uintL)(getpid()); // ca. 8 Bit von der Process ID
93 #elif defined(__OpenBSD__)
94 seed_lo = arc4random();
95 seed_hi = arc4random();
98 seed_hi = (rand() << 8) ^ (uintL)(GetCurrentProcessId());
99 #elif defined(__atarist)
100 seed_lo = highlow32(GEMDOS_GetDate(),GEMDOS_GetTime()); // 16+16 zufällige Bits
101 seed_hi = XBIOS_Random(); // 24 Bit zufällig vom XBIOS, vorne 8 Nullbits
102 #elif defined(amiga) || defined(AMIGA)
103 seed_lo = get_real_time(); // Uhrzeit
104 seed_hi = FindTask(NULL); // Pointer auf eigene Task
105 #elif defined(__MSDOS__) || defined(__EMX__) || defined(__riscos)
106 // Keine Zufallszahlen, keine PID, nichts Zufälliges da.
107 seed_lo = get_real_time(); // Uhrzeit, 100 Hz
108 seed_hi = time(NULL);
110 #error "Must implement random_state constructor!"
112 seed_hi ^= counter++ << 5;