MCPcopy Create free account
hub / github.com/Kitware/CMake / RandomSeed

Method RandomSeed

Source/cmSystemTools.cxx:2919–2962  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2917#endif
2918
2919unsigned int cmSystemTools::RandomSeed()
2920{
2921#if defined(_WIN32) && !defined(__CYGWIN__)
2922 unsigned int seed = 0;
2923
2924 // Try using a real random source.
2925 if (WinCryptRandom(&seed, sizeof(seed))) {
2926 return seed;
2927 }
2928
2929 // Fall back to the time and pid.
2930 FILETIME ft;
2931 GetSystemTimeAsFileTime(&ft);
2932 unsigned int t1 = static_cast<unsigned int>(ft.dwHighDateTime);
2933 unsigned int t2 = static_cast<unsigned int>(ft.dwLowDateTime);
2934 unsigned int pid = static_cast<unsigned int>(GetCurrentProcessId());
2935 return t1 ^ t2 ^ pid;
2936#else
2937 union
2938 {
2939 unsigned int integer;
2940 char bytes[sizeof(unsigned int)];
2941 } seed;
2942
2943 // Try using a real random source.
2944 cmsys::ifstream fin;
2945 fin.rdbuf()->pubsetbuf(nullptr, 0); // Unbuffered read.
2946 fin.open("/dev/urandom");
2947 if (fin.good() && fin.read(seed.bytes, sizeof(seed)) &&
2948 fin.gcount() == sizeof(seed)) {
2949 return seed.integer;
2950 }
2951
2952 // Fall back to the time and pid.
2953 struct timeval t;
2954 gettimeofday(&t, nullptr);
2955 unsigned int pid = static_cast<unsigned int>(getpid());
2956 unsigned int tv_sec = static_cast<unsigned int>(t.tv_sec);
2957 unsigned int tv_usec = static_cast<unsigned int>(t.tv_usec);
2958 // Since tv_usec never fills more than 11 bits we shift it to fill
2959 // in the slow-changing high-order bits of tv_sec.
2960 return tv_sec ^ (tv_usec << 21) ^ pid;
2961#endif
2962}
2963
2964unsigned int cmSystemTools::RandomNumber()
2965{

Callers

nothing calls this directly

Calls 4

WinCryptRandomFunction · 0.85
goodMethod · 0.80
openMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected