1 // cl_random_state constructor.
12 #include "cl_base_config.h"
15 #if defined(unix) || defined(__unix) || defined(_AIX) || defined(sinix) || (defined(_WIN32) && defined(__GNUC__))
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 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;
73 // Counter, to avoid that two random-states created immediately one after
74 // the other contain the same seed.
75 static uint32 counter = 0;
77 cl_random_state::cl_random_state ()
81 #if defined(unix) || defined(__unix) || defined(_AIX) || defined(sinix) || (defined(_WIN32) && defined(__GNUC__))
83 seed_hi = (rand() // zufällige 31 Bit (bei UNIX_BSD) bzw. 16 Bit (bei UNIX_SYSV)
84 << 8) ^ (uintL)(getpid()); // ca. 8 Bit von der Process ID
85 #elif defined(__atarist)
86 seed_lo = highlow32(GEMDOS_GetDate(),GEMDOS_GetTime()); // 16+16 zufällige Bits
87 seed_hi = XBIOS_Random(); // 24 Bit zufällig vom XBIOS, vorne 8 Nullbits
88 #elif defined(amiga) || defined(AMIGA)
89 seed_lo = get_real_time(); // Uhrzeit
90 seed_hi = FindTask(NULL); // Pointer auf eigene Task
91 #elif defined(__MSDOS__) || defined(__EMX__) || defined(__riscos)
92 // Keine Zufallszahlen, keine PID, nichts Zufälliges da.
93 seed_lo = get_real_time(); // Uhrzeit, 100 Hz
96 #error "Must implement cl_random_state constructor!"
98 seed_hi ^= counter++ << 5;