summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2016-10-30 10:49:22 +0100
committerGitHub <noreply@github.com>2016-10-30 10:49:22 +0100
commit89001369e7d5b6690700e62c80027979b1150411 (patch)
tree519c1336bc2fd2dc9cffbdfe567a09afab352ce3
parent426ff9e7ac6acc920a80a37807e93e27563310e9 (diff)
parenta747c8cceea53cd6dd02f4c41f062549afbbcac1 (diff)
Merge pull request #6751 from SuperUserNameMan/new_PRNG_for_rand_from_seed
xorshift32 PRNG for Math::rand_from_seed()
-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) {