summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryg2f <yoann@terminajones.com>2016-10-09 02:21:44 +0200
committeryg2f <yoann@terminajones.com>2016-10-10 20:41:36 +0200
commita747c8cceea53cd6dd02f4c41f062549afbbcac1 (patch)
treeac1cd178be37a0571b46bb50d6d97cfd8826e48a
parent1f9e16119f2b17fa507bdee8529459ed91f27b8c (diff)
xorshift32 PRNG for Math::rand_from_seed()
should fixes #1043 PRNG adapted from : http://excamera.com/sphinx/article-xorshift.html
-rw-r--r--core/math/math_funcs.cpp37
1 files changed, 8 insertions, 29 deletions
diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp
index 64615fe6b4..46c0218707 100644
--- a/core/math/math_funcs.cpp
+++ b/core/math/math_funcs.cpp
@@ -41,37 +41,16 @@ static uint32_t Q[4096];
#endif
uint32_t Math::rand_from_seed(uint32_t *seed) {
-
-#if 1
- uint32_t k;
- uint32_t s = (*seed);
- if (s == 0)
- s = 0x12345987;
- k = s / 127773;
- s = 16807 * (s - k * 127773) - 2836 * k;
-// if (s < 0)
-// s += 2147483647;
- (*seed) = s;
- return (s & Math::RANDOM_MAX);
-#else
- *seed = *seed * 1103515245 + 12345;
- return (*seed % ((unsigned int)RANDOM_MAX + 1));
-#endif
+ // Xorshift31 PRNG
+ if ( *seed == 0 ) *seed = Math::RANDOM_MAX;
+ (*seed) ^= (*seed) << 13;
+ (*seed) ^= (*seed) >> 17;
+ (*seed) ^= (*seed) << 5;
+ return (*seed) & Math::RANDOM_MAX;
}
void Math::seed(uint32_t x) {
-#if 0
- int i;
-
- Q[0] = x;
- Q[1] = x + PHI;
- Q[2] = x + PHI + PHI;
-
- for (i = 3; i < 4096; i++)
- Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
-#else
default_seed=x;
-#endif
}
void Math::randomize() {
@@ -82,12 +61,12 @@ void Math::randomize() {
uint32_t Math::rand() {
- return rand_from_seed(&default_seed)&0x7FFFFFFF;
+ return rand_from_seed(&default_seed);
}
double Math::randf() {
- return (double)rand() / (double)RANDOM_MAX;
+ return (double)rand() / (double)Math::RANDOM_MAX;
}
double Math::sin(double p_x) {