From 4c9004671af455a03acb4e2750b12d62b2b3c917 Mon Sep 17 00:00:00 2001 From: Ferenc Arn Date: Sat, 14 Jan 2017 23:34:51 -0600 Subject: [PATCH] Replace the existing PRNG (Xorshift31) with (minimal) PCG (XSH-RR variant with 32-bit output, 64-bit state). PCG is better than many alternatives by many metrics (see www.pcg-random.org) including statistical quality with good speed. --- core/math/math_funcs.cpp | 25 +++++++++---------- core/math/math_funcs.h | 7 +++--- core/math/pcg.cpp | 15 +++++++++++ core/math/pcg.h | 14 +++++++++++ doc/base/classes.xml | 10 ++++---- modules/gdscript/gd_functions.cpp | 4 +-- .../visual_script_builtin_funcs.cpp | 4 +-- 7 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 core/math/pcg.cpp create mode 100644 core/math/pcg.h diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp index 8353aa0ebe1..ef8c1ec539e 100644 --- a/core/math/math_funcs.cpp +++ b/core/math/math_funcs.cpp @@ -31,8 +31,9 @@ #include "core/os/os.h" #include "float.h" -uint32_t Math::default_seed=1; +#include "pcg.h" +pcg32_random_t Math::default_pcg = {1, PCG_DEFAULT_INC_64}; #define PHI 0x9e3779b9 @@ -40,28 +41,26 @@ uint32_t Math::default_seed=1; static uint32_t Q[4096]; #endif -uint32_t Math::rand_from_seed(uint32_t *seed) { - // Xorshift31 PRNG - if ( *seed == 0 ) *seed = Math::RANDOM_MAX; - (*seed) ^= (*seed) << 13; - (*seed) ^= (*seed) >> 17; - (*seed) ^= (*seed) << 5; - return (*seed) & Math::RANDOM_MAX; +// TODO: we should eventually expose pcg.inc too +uint32_t Math::rand_from_seed(uint64_t *seed) { + pcg32_random_t pcg = {*seed, PCG_DEFAULT_INC_64}; + uint32_t r = pcg32_random_r(&pcg); + *seed = pcg.state; + return r; } -void Math::seed(uint32_t x) { - default_seed=x; +void Math::seed(uint64_t x) { + default_pcg.state=x; } void Math::randomize() { OS::Time time = OS::get_singleton()->get_time(); - seed(OS::get_singleton()->get_ticks_usec()*(time.hour+1)*(time.min+1)*(time.sec+1)*rand()); /* *OS::get_singleton()->get_time().sec); // windows doesn't have get_time(), returns always 0 */ + seed(OS::get_singleton()->get_ticks_usec()*(time.hour+1)*(time.min+1)*(time.sec+1)*rand()); // TODO: can be simplified. } uint32_t Math::rand() { - - return rand_from_seed(&default_seed); + return pcg32_random_r(&default_pcg); } double Math::randf() { diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 8ce59224ff4..e81646b1cab 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -31,6 +31,7 @@ #include "typedefs.h" #include "math_defs.h" +#include "pcg.h" #ifndef NO_MATH_H #include @@ -41,8 +42,8 @@ class Math { + static pcg32_random_t default_pcg; - static uint32_t default_seed; public: Math() {} // useless to instance @@ -150,12 +151,12 @@ public: } - static uint32_t rand_from_seed(uint32_t *seed); + static uint32_t rand_from_seed(uint64_t *seed); static double ease(double p_x, double p_c); static int step_decimals(double p_step); static double stepify(double p_value,double p_step); - static void seed(uint32_t x=0); + static void seed(uint64_t x=0); static void randomize(); static uint32_t larger_prime(uint32_t p_val); static double dectime(double p_value,double p_amount, double p_step); diff --git a/core/math/pcg.cpp b/core/math/pcg.cpp new file mode 100644 index 00000000000..eac3b36d368 --- /dev/null +++ b/core/math/pcg.cpp @@ -0,0 +1,15 @@ +// *Really* minimal PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org +// Licensed under Apache License 2.0 (NO WARRANTY, etc. see website) + +#include "pcg.h" + +uint32_t pcg32_random_r(pcg32_random_t* rng) +{ + uint64_t oldstate = rng->state; + // Advance internal state + rng->state = oldstate * 6364136223846793005ULL + (rng->inc|1); + // Calculate output function (XSH RR), uses old state for max ILP + uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u; + uint32_t rot = oldstate >> 59u; + return (xorshifted >> rot) | (xorshifted << ((-rot) & 31)); +} diff --git a/core/math/pcg.h b/core/math/pcg.h new file mode 100644 index 00000000000..81f4c9770e8 --- /dev/null +++ b/core/math/pcg.h @@ -0,0 +1,14 @@ +// *Really* minimal PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org +// Licensed under Apache License 2.0 (NO WARRANTY, etc. see website) + +#ifndef RANDOM_H +#define RANDOM_H + +#include "typedefs.h" + +#define PCG_DEFAULT_INC_64 1442695040888963407ULL + +typedef struct { uint64_t state; uint64_t inc; } pcg32_random_t; +uint32_t pcg32_random_r(pcg32_random_t* rng); + +#endif // RANDOM_H diff --git a/doc/base/classes.xml b/doc/base/classes.xml index 0088f576f02..0b8c5cc11ce 100644 --- a/doc/base/classes.xml +++ b/doc/base/classes.xml @@ -463,7 +463,7 @@ - Random range, any floating point value between 'from' and 'to' + Random range, any floating point value between 'from' and 'to'. @@ -472,28 +472,28 @@ - Random from seed, pass a seed and an array with both number and new seed is returned. + Random from seed: pass a seed, and an array with both number and new seed is returned. "Seed" here refers to the internal state of the pseudo random number generator. The internal state of the current implementation is 64 bits. - Random value (0 to 1 float). + Return a random floating point value between 0 and 1. - Random 32 bits value (integer). To obtain a value from 0 to N, you can use remainder, like (for random from 0 to 19): randi() % 20. + Return a random 32 bits integer value. To obtain a random value between 0 to N (where N is smaller than 2^32 - 1), you can use remainder. For example, to get a random integer between 0 and 19 inclusive, you can use randi() % 20. - Reset the seed of the random number generator with a new, different one. + Randomize the seed (or the internal state) of the random number generator. Current implementation reseeds using a number based on time. diff --git a/modules/gdscript/gd_functions.cpp b/modules/gdscript/gd_functions.cpp index 1c41b2e73bd..f0da1ea8cc4 100644 --- a/modules/gdscript/gd_functions.cpp +++ b/modules/gdscript/gd_functions.cpp @@ -351,14 +351,14 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va case MATH_SEED: { VALIDATE_ARG_COUNT(1); VALIDATE_ARG_NUM(0); - uint32_t seed=*p_args[0]; + uint64_t seed=*p_args[0]; Math::seed(seed); r_ret=Variant(); } break; case MATH_RANDSEED: { VALIDATE_ARG_COUNT(1); VALIDATE_ARG_NUM(0); - uint32_t seed=*p_args[0]; + uint64_t seed=*p_args[0]; int ret = Math::rand_from_seed(&seed); Array reta; reta.push_back(ret); diff --git a/modules/visual_script/visual_script_builtin_funcs.cpp b/modules/visual_script/visual_script_builtin_funcs.cpp index 27242384ac9..2a14e62a520 100644 --- a/modules/visual_script/visual_script_builtin_funcs.cpp +++ b/modules/visual_script/visual_script_builtin_funcs.cpp @@ -802,14 +802,14 @@ void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func,const Variant** p_inp case VisualScriptBuiltinFunc::MATH_SEED: { VALIDATE_ARG_NUM(0); - uint32_t seed=*p_inputs[0]; + uint64_t seed=*p_inputs[0]; Math::seed(seed); } break; case VisualScriptBuiltinFunc::MATH_RANDSEED: { VALIDATE_ARG_NUM(0); - uint32_t seed=*p_inputs[0]; + uint64_t seed=*p_inputs[0]; int ret = Math::rand_from_seed(&seed); Array reta; reta.push_back(ret);