1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-25 01:18:38 -05:00

qrtplib: simplify random functions

This commit is contained in:
f4exb 2018-03-05 01:43:22 +01:00
parent afe3fbabd3
commit e86e2f25a2
3 changed files with 10 additions and 61 deletions

View File

@ -38,18 +38,11 @@
#include "rtprandomrands.h"
#include "rtprandomurandom.h"
#include "rtprandomrand48.h"
#include <time.h>
#ifndef WIN32
#include <unistd.h>
#else
#ifndef _WIN32_WCE
#include <process.h>
#else
#include <windows.h>
#include <kfuncs.h>
#endif // _WIN32_WINCE
#include <stdlib.h>
#endif // WIN32
#include <QDateTime>
namespace qrtplib
{
@ -57,39 +50,21 @@ namespace qrtplib
uint32_t RTPRandom::PickSeed()
{
uint32_t x;
#if defined(WIN32) || defined(_WIN32_WINCE)
#ifndef _WIN32_WCE
x = (uint32_t)_getpid();
x += (uint32_t)time(0);
x += (uint32_t)clock();
#else
x = (uint32_t)GetCurrentProcessId();
FILETIME ft;
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st,&ft);
x += ft.dwLowDateTime;
#endif // _WIN32_WCE
x ^= (uint32_t)((uint8_t *)this - (uint8_t *)0);
#else
x = (uint32_t) getpid();
x += (uint32_t) time(0);
x += (uint32_t) clock();
x ^= (uint32_t) ((uint8_t *) this - (uint8_t *) 0);
QDateTime currentDateTime = QDateTime::currentDateTime();
x += currentDateTime.toTime_t();
#if defined(WIN32)
x += QDateTime::currentMSecsSinceEpoch() % 1000;
#else
x += (uint32_t)clock();
#endif
x ^= (uint32_t)((uint8_t *)this - (uint8_t *)0);
return x;
}
RTPRandom *RTPRandom::CreateDefaultRandomNumberGenerator()
{
#ifdef RTP_HAVE_RAND_S
RTPRandomRandS *r = new RTPRandomRandS();
#else
RTPRandomURandom *r = new RTPRandomURandom();
#endif // RTP_HAVE_RAND_S
RTPRandom *rRet = r;
if (r->Init() < 0) // fall back to rand48

View File

@ -51,12 +51,7 @@ RTPRandomRand48::~RTPRandomRand48()
void RTPRandomRand48::SetSeed(uint32_t seed)
{
#ifdef RTP_HAVE_VSUINT64SUFFIX
state = ((uint64_t)seed) << 16 | 0x330Eui64;
#else
state = ((uint64_t) seed) << 16 | 0x330EULL;
#endif // RTP_HAVE_VSUINT64SUFFIX
}
uint8_t RTPRandomRand48::GetRandom8()
@ -75,16 +70,8 @@ uint16_t RTPRandomRand48::GetRandom16()
uint32_t RTPRandomRand48::GetRandom32()
{
#ifdef RTP_HAVE_VSUINT64SUFFIX
state = ((0x5DEECE66Dui64*state) + 0xBui64)&0x0000ffffffffffffui64;
uint32_t x = (uint32_t)((state>>16)&0xffffffffui64);
#else
state = ((0x5DEECE66DULL * state) + 0xBULL) & 0x0000ffffffffffffULL;
uint32_t x = (uint32_t) ((state >> 16) & 0xffffffffULL);
#endif // RTP_HAVE_VSUINT64SUFFIX
return x;
}
@ -92,15 +79,8 @@ uint32_t RTPRandomRand48::GetRandom32()
double RTPRandomRand48::GetRandomDouble()
{
#ifdef RTP_HAVE_VSUINT64SUFFIX
state = ((0x5DEECE66Dui64*state) + 0xBui64)&0x0000ffffffffffffui64;
int64_t x = (int64_t)state;
#else
state = ((0x5DEECE66DULL * state) + 0xBULL) & 0x0000ffffffffffffULL;
int64_t x = (int64_t) state;
#endif // RTP_HAVE_VSUINT64SUFFIX
double y = 3.552713678800500929355621337890625e-15 * (double) x;
return y;

View File

@ -116,17 +116,11 @@ double RTPRandomURandom::GetRandomDouble()
return 0;
}
#ifdef RTP_HAVE_VSUINT64SUFFIX
value &= 0x7fffffffffffffffui64;
#else
value &= 0x7fffffffffffffffULL;
#endif // RTP_HAVE_VSUINT64SUFFIX
int64_t value2 = (int64_t) value;
double x = RTPRANDOM_2POWMIN63 * (double) value2;
return x;
}
} // end namespace