mirror of
git://git.musl-libc.org/musl
synced 2024-12-26 00:22:35 +00:00
prng: fix rand() to give good sequence with small state
some applications rely on the low bits of rand() to be reasonably good quality prng, so now it fixed by using the top bits of a 64 bit LCG, this is simple, has small state and passes statistical tests. D.E. Knuth attributes the multiplier to C.E. Haynes in TAOCP Vol2 3.3.4
This commit is contained in:
parent
fd1d7be35f
commit
c79cd27e9e
@ -1,6 +1,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static unsigned seed;
|
||||
static uint64_t seed;
|
||||
|
||||
void srand(unsigned s)
|
||||
{
|
||||
@ -9,5 +10,6 @@ void srand(unsigned s)
|
||||
|
||||
int rand(void)
|
||||
{
|
||||
return (seed = (seed+1) * 1103515245 + 12345 - 1)+1 & 0x7fffffff;
|
||||
seed = 6364136223846793005ULL*seed + 1;
|
||||
return seed>>33;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user