00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <globalsearch/random.h>
00016
00017 #include <cstdlib>
00018 #include <ctime>
00019
00020 #ifdef WIN32
00021 #include <process.h>
00022 #define GETPID _getpid
00023 #else // WIN32
00024 #include <unistd.h>
00025 #define GETPID getpid
00026 #endif // WIN32
00027
00028 unsigned long getSeed()
00029 {
00030 unsigned long a,b,c;
00031 a = std::clock();
00032 b = std::time(0);
00033 c = GETPID();
00034 a=a-b; a=a-c; a=a^(c >> 13);
00035 b=b-c; b=b-a; b=b^(a << 8);
00036 c=c-a; c=c-b; c=c^(b >> 13);
00037 a=a-b; a=a-c; a=a^(c >> 12);
00038 b=b-c; b=b-a; b=b^(a << 16);
00039 c=c-a; c=c-b; c=c^(b >> 5);
00040 a=a-b; a=a-c; a=a^(c >> 3);
00041 b=b-c; b=b-a; b=b^(a << 10);
00042 c=c-a; c=c-b; c=c^(b >> 15);
00043 return c;
00044 }
00045
00046 namespace GlobalSearch {
00047
00048 GSRandom* GSRandom::m_instance = 0;
00049
00050 GSRandom* GSRandom::instance()
00051 {
00052 if (!m_instance) {
00053 m_instance = new GSRandom();
00054 }
00055 return m_instance;
00056 }
00057
00058 GSRandom::GSRandom() :
00059 m_seedLock(false)
00060 {
00061 while (m_seedLock) {};
00062 m_seedLock = true;
00063 std::srand(getSeed());
00064 m_seedLock = false;
00065 }
00066
00067 double GSRandom::getRandomDouble()
00068 {
00069 while (m_seedLock) {};
00070 m_seedLock = true;
00071 double d = rand() / (double)RAND_MAX;
00072 m_seedLock = false;
00073 return d;
00074 }
00075
00076 unsigned int GSRandom::getRandomUInt()
00077 {
00078 while (m_seedLock) {};
00079 m_seedLock = true;
00080 unsigned int i = rand();
00081 m_seedLock = false;
00082 return i;
00083 }
00084
00085 }