Improve RNG
This commit is contained in:
1633
src/libthecore/XoshiroCpp.hpp
Normal file
1633
src/libthecore/XoshiroCpp.hpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -6,6 +6,8 @@
|
||||
*/
|
||||
#define __LIBTHECORE__
|
||||
#include "stdafx.h"
|
||||
#include <random>
|
||||
#include "XoshiroCpp.hpp"
|
||||
|
||||
static struct timeval null_time = { 0, 0 };
|
||||
|
||||
@@ -344,15 +346,6 @@ int MINMAX(int min, int value, int max)
|
||||
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)
|
||||
{
|
||||
if (from > to)
|
||||
@@ -367,8 +360,13 @@ int number_ex(int from, int to, const char *file, int line)
|
||||
|
||||
int returnValue = 0;
|
||||
|
||||
std::random_device rd;
|
||||
XoshiroCpp::Xoshiro128PlusPlus gen(rd());
|
||||
|
||||
std::uniform_int_distribution<> distrib(from, to);
|
||||
|
||||
if ((to - from + 1) != 0)
|
||||
returnValue = ((thecore_random() % (to - from + 1)) + from);
|
||||
returnValue = distrib(gen);
|
||||
else
|
||||
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)
|
||||
{
|
||||
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
|
||||
|
||||
@@ -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__)
|
||||
|
||||
float fnumber(float from, float to);
|
||||
extern float gauss_random(float avg = 0, float sigma = 1);
|
||||
|
||||
void thecore_sleep(struct timeval * timeout);
|
||||
DWORD thecore_random();
|
||||
|
||||
float get_float_time();
|
||||
DWORD get_dword_time();
|
||||
|
||||
Reference in New Issue
Block a user