rand: switch to 'Numerical Recipes' constants and remove random8 to increase period

pull/25/head
Tomas Susanka 6 years ago
parent c5227fdb96
commit 54727e6650

@ -35,22 +35,13 @@
// You are supposed to replace the random8() and random32() function with your own secure code. // You are supposed to replace the random8() and random32() function with your own secure code.
// There is also a possibility to replace the random_buffer() function as it is defined as a weak symbol. // There is also a possibility to replace the random_buffer() function as it is defined as a weak symbol.
static uint8_t random8(void)
{
// Linear congruential generator used in glibc
// https://en.wikipedia.org/wiki/Linear_congruential_generator
static int seed = 0;
seed = (1103515245 * seed + 12345) & 0x7FFFFFFF;
return seed & 0xFF;
}
uint32_t random32(void) uint32_t random32(void)
{ {
uint32_t r1 = random8(); // Linear congruential generator used in glibc
uint32_t r2 = random8(); // https://en.wikipedia.org/wiki/Linear_congruential_generator
uint32_t r3 = random8(); static uint32_t seed = 0;
uint32_t r4 = random8(); seed = 1664525 * seed + 1013904223;
return ((r1 << 24) | (r2 << 16) | (r3 << 8) | r4); return seed;
} }
#endif /* RAND_PLATFORM_INDEPENDENT */ #endif /* RAND_PLATFORM_INDEPENDENT */

Loading…
Cancel
Save