1
0
Fork 0

fixed random number generator initialization

This commit is contained in:
Jan Steemann 2016-08-02 12:25:46 +02:00
parent 2870a9f10f
commit 8e591c16c2
2 changed files with 14 additions and 17 deletions

View File

@ -143,11 +143,7 @@ class HybridLogicalClock {
}
return r;
}
private:
// helper to compute the offset between epoch and 1970
uint64_t computeOffset1970();
// helper to get the physical time in milliseconds since the epoch:
uint64_t getPhysicalTime() {
auto now = _clock.now();
@ -158,6 +154,10 @@ class HybridLogicalClock {
return ms;
}
private:
// helper to compute the offset between epoch and 1970
uint64_t computeOffset1970();
static uint64_t extractTime(uint64_t t) { return t >> 20; }
static uint64_t extractCount(uint64_t t) { return t & 0xfffffUL; }

View File

@ -31,7 +31,9 @@
#endif
#include "Basics/Exceptions.h"
#include "Basics/HybridLogicalClock.h"
#include "Basics/Thread.h"
#include "Basics/hashes.h"
#include "Logger/Logger.h"
using namespace arangodb;
@ -42,16 +44,12 @@ using namespace arangodb::basics;
// -----------------------------------------------------------------------------
unsigned long RandomDevice::seed() {
unsigned long s = (unsigned long)time(0);
struct timeval tv;
int result = gettimeofday(&tv, 0);
s ^= static_cast<unsigned long>(tv.tv_sec);
s ^= static_cast<unsigned long>(tv.tv_usec);
s ^= static_cast<unsigned long>(result);
s ^= static_cast<unsigned long>(Thread::currentProcessId());
HybridLogicalClock clock;
unsigned long s = static_cast<unsigned long>(clock.getPhysicalTime());
TRI_pid_t pid = Thread::currentProcessId();
s ^= static_cast<unsigned long>(TRI_Crc32HashPointer(&pid, sizeof(TRI_pid_t)));
s = static_cast<unsigned long>(TRI_Crc32HashPointer(&s, sizeof(unsigned long)));
return s;
}
@ -76,7 +74,7 @@ int32_t RandomDevice::random(int32_t left, int32_t right) {
}
uint32_t range = static_cast<uint32_t>(right - left + 1);
switch (range) {
case 0x00000002:
return power2(left, 0x00000002 - 1);
@ -353,8 +351,7 @@ class RandomDeviceCombined : public RandomDevice {
class RandomDeviceMersenne : public RandomDevice {
public:
RandomDeviceMersenne()
: engine(static_cast<unsigned int>(
std::chrono::system_clock::now().time_since_epoch().count())) {}
: engine(RandomDevice::seed()) {}
uint32_t random() { return engine(); }