summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2019-03-07 11:18:18 +0100
committerGitHub <noreply@github.com>2019-03-07 11:18:18 +0100
commiteb4a6a8147ad268802f59a9a2273a39e3da4a35e (patch)
tree9d1005aa1a10dc0184f3b0d6d2ccff6766229b24
parent8f5d9b63914b0049bd202b87c4b89ff3d48ad0ad (diff)
parent4eccb58bc57ac66f929192066b65282a56d46545 (diff)
Merge pull request #26737 from MidZik/rng-seed-fix
Fixed get_seed() not returning the correct seed.
-rw-r--r--core/math/random_pcg.cpp14
-rw-r--r--core/math/random_pcg.h21
2 files changed, 20 insertions, 15 deletions
diff --git a/core/math/random_pcg.cpp b/core/math/random_pcg.cpp
index 8bbcca88fe..8c324414e6 100644
--- a/core/math/random_pcg.cpp
+++ b/core/math/random_pcg.cpp
@@ -32,24 +32,24 @@
#include "core/os/os.h"
-RandomPCG::RandomPCG(uint64_t seed, uint64_t inc) :
+RandomPCG::RandomPCG(uint64_t p_seed, uint64_t p_inc) :
pcg() {
- pcg.state = seed;
- pcg.inc = inc;
+ pcg.inc = p_inc;
+ seed(p_seed);
}
void RandomPCG::randomize() {
seed(OS::get_singleton()->get_ticks_usec() * pcg.state + PCG_DEFAULT_INC_64);
}
-double RandomPCG::random(double from, double to) {
+double RandomPCG::random(double p_from, double p_to) {
unsigned int r = rand();
double ret = (double)r / (double)RANDOM_MAX;
- return (ret) * (to - from) + from;
+ return (ret) * (p_to - p_from) + p_from;
}
-float RandomPCG::random(float from, float to) {
+float RandomPCG::random(float p_from, float p_to) {
unsigned int r = rand();
float ret = (float)r / (float)RANDOM_MAX;
- return (ret) * (to - from) + from;
+ return (ret) * (p_to - p_from) + p_from;
}
diff --git a/core/math/random_pcg.h b/core/math/random_pcg.h
index 2a69d43904..f6cc3db595 100644
--- a/core/math/random_pcg.h
+++ b/core/math/random_pcg.h
@@ -37,28 +37,33 @@
class RandomPCG {
pcg32_random_t pcg;
+ uint64_t current_seed = DEFAULT_SEED; // seed with this to get the same state
public:
static const uint64_t DEFAULT_SEED = 12047754176567800795U;
static const uint64_t DEFAULT_INC = PCG_DEFAULT_INC_64;
static const uint64_t RANDOM_MAX = 0xFFFFFFFF;
- RandomPCG(uint64_t seed = DEFAULT_SEED, uint64_t inc = PCG_DEFAULT_INC_64);
+ RandomPCG(uint64_t p_seed = DEFAULT_SEED, uint64_t p_inc = PCG_DEFAULT_INC_64);
- _FORCE_INLINE_ void seed(uint64_t seed) {
- pcg.state = seed;
+ _FORCE_INLINE_ void seed(uint64_t p_seed) {
+ current_seed = p_seed;
+ pcg.state = p_seed;
pcg32_random_r(&pcg); // Force changing internal state to avoid initial 0
}
- _FORCE_INLINE_ uint64_t get_seed() { return pcg.state; }
+ _FORCE_INLINE_ uint64_t get_seed() { return current_seed; }
void randomize();
- _FORCE_INLINE_ uint32_t rand() { return pcg32_random_r(&pcg); }
+ _FORCE_INLINE_ uint32_t rand() {
+ current_seed = pcg.state;
+ return pcg32_random_r(&pcg);
+ }
_FORCE_INLINE_ double randd() { return (double)rand() / (double)RANDOM_MAX; }
_FORCE_INLINE_ float randf() { return (float)rand() / (float)RANDOM_MAX; }
- double random(double from, double to);
- float random(float from, float to);
- real_t random(int from, int to) { return (real_t)random((real_t)from, (real_t)to); }
+ double random(double p_from, double p_to);
+ float random(float p_from, float p_to);
+ real_t random(int p_from, int p_to) { return (real_t)random((real_t)p_from, (real_t)p_to); }
};
#endif // RANDOM_PCG_H