Changeset 1005 for cpp/common/random.h


Ignore:
Timestamp:
07/14/20 15:54:43 (4 years ago)
Author:
Maciej Komosinski
Message:

Higher conformance with C++17, but gave up after missing M_PI, M_PI_2, strdup() and more; other cosmetic improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/random.h

    r896 r1005  
    66
    77#ifdef _MSC_VER
    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
     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
    1111#endif
    1212
     
    1414#include <stdint.h> //uintptr_t
    1515#ifdef MULTITHREADED
    16  #include "threads.h"
     16#include "threads.h"
    1717#endif
    1818#ifdef LINUX
    19  #include <unistd.h>
    20  #include <sys/stat.h>
    21  #include <fcntl.h>
     19#include <unistd.h>
     20#include <sys/stat.h>
     21#include <fcntl.h>
    2222#endif
    2323#ifdef _WIN32
    24  #define _WINSOCKAPI_ //http://stackoverflow.com/questions/1372480/c-redefinition-header-files
    25  #include <rpc.h> //UUID
    26  #pragma comment(lib, "Rpcrt4.lib")
     24#define _WINSOCKAPI_ //http://stackoverflow.com/questions/1372480/c-redefinition-header-files
     25#include <rpc.h> //UUID
     26#pragma comment(lib, "Rpcrt4.lib")
    2727#endif
    2828
     
    6363                mt[0] = seed;
    6464                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;
    6666#ifdef MULTITHREADED
    6767                pthread_mutex_unlock(&lock);
     
    7474                //for ms visual, could use http://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx
    7575#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)
    7878                {
    79                         read(fd,&seed,sizeof(seed));
     79                        read(fd, &seed, sizeof(seed));
    8080                        close(fd);
    8181                }
     
    8484                {
    8585                        counter++;
    86                         seed = (unsigned int)time(NULL);                         //time (seconds); could use hi-res timer but then we would depend on common/timer.h
     86                        seed = (unsigned int)time(NULL);           //time (seconds); could use hi-res timer but then we would depend on common/timer.h
    8787                        seed ^= counter;                           //incremented value, possibly randomly initialized
    8888                        seed ^= (unsigned int)(uintptr_t)&counter; //memory address
     
    9191                UUID uuid;
    9292                ::UuidCreate(&uuid);
    93                 seed ^= uuid.Data1^uuid.Data2^uuid.Data3^uuid.Data4[0];
     93                seed ^= uuid.Data1 ^ uuid.Data2 ^ uuid.Data3 ^ uuid.Data4[0];
    9494#endif
    9595                setSeed(seed);
     
    117117        //UniformRandomBitGenerator
    118118        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(); }
    122122
    123123        inline double getDouble() // [0,1)
Note: See TracChangeset for help on using the changeset viewer.