Merge pull request #6751 from SuperUserNameMan/new_PRNG_for_rand_from_seed
xorshift32 PRNG for Math::rand_from_seed()
This commit is contained in:
commit
89001369e7
1 changed files with 8 additions and 29 deletions
|
@ -41,37 +41,16 @@ static uint32_t Q[4096];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uint32_t Math::rand_from_seed(uint32_t *seed) {
|
uint32_t Math::rand_from_seed(uint32_t *seed) {
|
||||||
|
// Xorshift31 PRNG
|
||||||
#if 1
|
if ( *seed == 0 ) *seed = Math::RANDOM_MAX;
|
||||||
uint32_t k;
|
(*seed) ^= (*seed) << 13;
|
||||||
uint32_t s = (*seed);
|
(*seed) ^= (*seed) >> 17;
|
||||||
if (s == 0)
|
(*seed) ^= (*seed) << 5;
|
||||||
s = 0x12345987;
|
return (*seed) & Math::RANDOM_MAX;
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Math::seed(uint32_t x) {
|
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;
|
default_seed=x;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Math::randomize() {
|
void Math::randomize() {
|
||||||
|
@ -82,12 +61,12 @@ void Math::randomize() {
|
||||||
|
|
||||||
uint32_t Math::rand() {
|
uint32_t Math::rand() {
|
||||||
|
|
||||||
return rand_from_seed(&default_seed)&0x7FFFFFFF;
|
return rand_from_seed(&default_seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
double Math::randf() {
|
double Math::randf() {
|
||||||
|
|
||||||
return (double)rand() / (double)RANDOM_MAX;
|
return (double)rand() / (double)Math::RANDOM_MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Math::sin(double p_x) {
|
double Math::sin(double p_x) {
|
||||||
|
|
Loading…
Reference in a new issue