Changeset 1005 for cpp/common/random.h
- Timestamp:
- 07/14/20 15:54:43 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified cpp/common/random.h ¶
r896 r1005 6 6 7 7 #ifdef _MSC_VER 8 9 10 8 #define NOMINMAX //we don't want Windows headers (minwindef.h) to define min() and max() as macros 9 #undef min //for some reason, NOMINMAX did not work so we have to #undef anyway 10 #undef max 11 11 #endif 12 12 … … 14 14 #include <stdint.h> //uintptr_t 15 15 #ifdef MULTITHREADED 16 16 #include "threads.h" 17 17 #endif 18 18 #ifdef LINUX 19 20 21 19 #include <unistd.h> 20 #include <sys/stat.h> 21 #include <fcntl.h> 22 22 #endif 23 23 #ifdef _WIN32 24 25 26 24 #define _WINSOCKAPI_ //http://stackoverflow.com/questions/1372480/c-redefinition-header-files 25 #include <rpc.h> //UUID 26 #pragma comment(lib, "Rpcrt4.lib") 27 27 #endif 28 28 … … 63 63 mt[0] = seed; 64 64 for (unsigned int i = 1; i < length; i++) 65 mt[i] = (1812433253 * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i) &bitMask_32;65 mt[i] = (1812433253 * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i) & bitMask_32; 66 66 #ifdef MULTITHREADED 67 67 pthread_mutex_unlock(&lock); … … 74 74 //for ms visual, could use http://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx 75 75 #ifdef LINUX 76 int fd =open("/dev/urandom",O_RDONLY);77 if (fd >=0)76 int fd = open("/dev/urandom", O_RDONLY); 77 if (fd >= 0) 78 78 { 79 read(fd, &seed,sizeof(seed));79 read(fd, &seed, sizeof(seed)); 80 80 close(fd); 81 81 } … … 84 84 { 85 85 counter++; 86 seed = (unsigned int)time(NULL); 86 seed = (unsigned int)time(NULL); //time (seconds); could use hi-res timer but then we would depend on common/timer.h 87 87 seed ^= counter; //incremented value, possibly randomly initialized 88 88 seed ^= (unsigned int)(uintptr_t)&counter; //memory address … … 91 91 UUID uuid; 92 92 ::UuidCreate(&uuid); 93 seed ^= uuid.Data1 ^uuid.Data2^uuid.Data3^uuid.Data4[0];93 seed ^= uuid.Data1 ^ uuid.Data2 ^ uuid.Data3 ^ uuid.Data4[0]; 94 94 #endif 95 95 setSeed(seed); … … 117 117 //UniformRandomBitGenerator 118 118 typedef unsigned int result_type; 119 static constexpr unsigned int min() { return 0;}120 static constexpr unsigned int max() { return MAXVALUE;}121 inline unsigned int operator()() { return getUint32();}119 static constexpr unsigned int min() { return 0; } 120 static constexpr unsigned int max() { return MAXVALUE; } 121 inline unsigned int operator()() { return getUint32(); } 122 122 123 123 inline double getDouble() // [0,1)
Note: See TracChangeset
for help on using the changeset viewer.