2015-12-22 02:48:00 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Burtle Prng - Modified. 42iterations instead of 20.
|
|
|
|
// ref: http://burtleburtle.net/bob/rand/smallprng.html
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#include "prng.h"
|
|
|
|
|
|
|
|
#define rot(x,k) (((x)<<(k))|((x)>>(32-(k))))
|
2019-03-10 18:20:22 +08:00
|
|
|
uint32_t burtle_get_mod(prng_ctx *x) {
|
2015-12-22 02:48:00 +08:00
|
|
|
uint32_t e = x->a - rot(x->b, 21);
|
|
|
|
x->a = x->b ^ rot(x->c, 19);
|
|
|
|
x->b = x->c + rot(x->d, 6);
|
|
|
|
x->c = x->d + e;
|
|
|
|
x->d = e + x->a;
|
|
|
|
return x->d;
|
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void burtle_init_mod(prng_ctx *x, uint32_t seed) {
|
2015-12-22 02:48:00 +08:00
|
|
|
x->a = 0xf1ea5eed;
|
2019-03-10 02:19:50 +08:00
|
|
|
x->b = x->c = x->d = seed;
|
2019-03-10 07:00:59 +08:00
|
|
|
for (uint8_t i = 0; i < 42; ++i) {
|
2015-12-22 02:48:00 +08:00
|
|
|
(void)burtle_get_mod(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void burtle_init(prng_ctx *x, uint32_t seed) {
|
2019-03-09 15:59:13 +08:00
|
|
|
uint32_t i;
|
2015-12-22 02:48:00 +08:00
|
|
|
x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
|
2019-03-10 07:00:59 +08:00
|
|
|
for (i = 0; i < 20; ++i) {
|
2015-12-22 02:48:00 +08:00
|
|
|
(void)burtle_get_mod(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
uint32_t GetSimplePrng(uint32_t seed) {
|
2019-03-10 02:19:50 +08:00
|
|
|
seed *= 0x19660D;
|
|
|
|
seed += 0x3C6EF35F;
|
|
|
|
return seed;
|
2015-12-22 02:48:00 +08:00
|
|
|
}
|