Improve RNG

This commit is contained in:
savis
2025-12-25 20:46:01 +01:00
parent 2663bb4c9c
commit ab382cac7c
7 changed files with 1663 additions and 54 deletions

View File

@@ -91,7 +91,7 @@ DWORD DESC_MANAGER::CreateHandshake()
RETRY: RETRY:
do do
{ {
DWORD val = thecore_random() % (1024 * 1024); DWORD val = number(0, (1024 * 1024));
*(DWORD *) (crc_buf ) = val; *(DWORD *) (crc_buf ) = val;
*((DWORD *) crc_buf + 1) = get_global_time(); *((DWORD *) crc_buf + 1) = get_global_time();
@@ -383,7 +383,7 @@ void DESC_MANAGER::GetUserCount(int & iTotal, int ** paiEmpireUserCount, int & i
DWORD DESC_MANAGER::MakeRandomKey(DWORD dwHandle) DWORD DESC_MANAGER::MakeRandomKey(DWORD dwHandle)
{ {
DWORD random_key = thecore_random(); DWORD random_key = number(0, INT_MAX);
m_map_handle_random_key.insert(std::make_pair(dwHandle, random_key)); m_map_handle_random_key.insert(std::make_pair(dwHandle, random_key));
return random_key; return random_key;
} }

View File

@@ -541,8 +541,8 @@ void CThreeWayWar::onDead(LPCHARACTER pChar, LPCHARACTER pkKiller)
int x = pChar->GetX(); int x = pChar->GetX();
int y = pChar->GetY(); int y = pChar->GetY();
x = (thecore_random() & 1) ? x - number(200, 1000) : x + number(200, 1000); x = (number(0, 1)) ? x - number(200, 1000) : x + number(200, 1000);
y = (thecore_random() & 1) ? y - number(200, 1000) : y + number(200, 1000); y = (number(0, 1)) ? y - number(200, 1000) : y + number(200, 1000);
if (x < 0) if (x < 0)
x = pChar->GetX(); x = pChar->GetX();

View File

@@ -26,7 +26,7 @@ int dice(int number, int size)
while (number) while (number)
{ {
val = ((thecore_random() % size) + 1); val = number(1, size);
sum += val; sum += val;
--number; --number;
} }
@@ -146,41 +146,6 @@ int CalculateDuration(int iSpd, int iDur)
return iDur * i / 100; return iDur * i / 100;
} }
double uniform_random(double a, double b)
{
// Uses 64-bit precision and the full range of the 32-bit DWORD
const double MAX_DIVISOR = 4294967296.0;
return (static_cast<double>(thecore_random()) / MAX_DIVISOR) * (b - a) + a;
//return thecore_random() / (RAND_MAX + 1.f) * (b - a) + a;
}
float gauss_random(float avg, float sigma)
{
static bool haveNextGaussian = false;
static float nextGaussian = 0.0f;
if (haveNextGaussian)
{
haveNextGaussian = false;
return nextGaussian * sigma + avg;
}
else
{
double v1, v2, s;
do {
//v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0
//v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0
v1 = uniform_random(-1.f, 1.f);
v2 = uniform_random(-1.f, 1.f);
s = v1 * v1 + v2 * v2;
} while (s >= 1.f || fabs(s) < FLT_EPSILON);
double multiplier = sqrtf(-2 * logf(s)/s);
nextGaussian = v2 * multiplier;
haveNextGaussian = true;
return v1 * multiplier * sigma + avg;
}
}
int parse_time_str(const char* str) int parse_time_str(const char* str)
{ {

View File

@@ -59,8 +59,6 @@ extern const char * first_cmd(const char *argument, char *first_arg, size_t firs
extern int CalculateDuration(int iSpd, int iDur); extern int CalculateDuration(int iSpd, int iDur);
extern float gauss_random(float avg = 0, float sigma = 1);
extern int parse_time_str(const char* str); extern int parse_time_str(const char* str);
extern bool WildCaseCmp(const char *w, const char *s); extern bool WildCaseCmp(const char *w, const char *s);

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,8 @@
*/ */
#define __LIBTHECORE__ #define __LIBTHECORE__
#include "stdafx.h" #include "stdafx.h"
#include <random>
#include "XoshiroCpp.hpp"
static struct timeval null_time = { 0, 0 }; static struct timeval null_time = { 0, 0 };
@@ -344,15 +346,6 @@ int MINMAX(int min, int value, int max)
return (max < tv) ? max : tv; return (max < tv) ? max : tv;
} }
DWORD thecore_random()
{
#ifdef OS_WINDOWS
return rand();
#else
return random();
#endif
}
int number_ex(int from, int to, const char *file, int line) int number_ex(int from, int to, const char *file, int line)
{ {
if (from > to) if (from > to)
@@ -367,8 +360,13 @@ int number_ex(int from, int to, const char *file, int line)
int returnValue = 0; int returnValue = 0;
std::random_device rd;
XoshiroCpp::Xoshiro128PlusPlus gen(rd());
std::uniform_int_distribution<> distrib(from, to);
if ((to - from + 1) != 0) if ((to - from + 1) != 0)
returnValue = ((thecore_random() % (to - from + 1)) + from); returnValue = distrib(gen);
else else
sys_err("number(): devided by 0"); sys_err("number(): devided by 0");
@@ -377,7 +375,22 @@ int number_ex(int from, int to, const char *file, int line)
float fnumber(float from, float to) float fnumber(float from, float to)
{ {
return (((float)thecore_random() / (float)RAND_MAX) * (to - from)) + from; std::random_device rd;
XoshiroCpp::Xoshiro128PlusPlus gen(rd());
std::uniform_real_distribution<float> distrib(from, to);
return distrib(gen);
}
float gauss_random(float avg, float sigma)
{
std::random_device rd;
XoshiroCpp::Xoshiro128PlusPlus gen(rd());
std::normal_distribution<float> distrib(avg, sigma);
return distrib(gen);
} }
#ifndef OS_WINDOWS #ifndef OS_WINDOWS

View File

@@ -41,9 +41,9 @@ int number_ex(int from, int to, const char *file, int line);
#define number(from, to) number_ex(from, to, __FILE__, __LINE__) #define number(from, to) number_ex(from, to, __FILE__, __LINE__)
float fnumber(float from, float to); float fnumber(float from, float to);
extern float gauss_random(float avg = 0, float sigma = 1);
void thecore_sleep(struct timeval * timeout); void thecore_sleep(struct timeval * timeout);
DWORD thecore_random();
float get_float_time(); float get_float_time();
DWORD get_dword_time(); DWORD get_dword_time();