musl - musl - an implementation of the standard library for Linux-based systems

index : musl
musl - an implementation of the standard library for Linux-based systems
summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorSzabolcs Nagy <nsz@port70.net>2013年06月08日 13:31:10 +0000
committerSzabolcs Nagy <nsz@port70.net>2013年06月08日 13:31:10 +0000
commitc79cd27e9e81eb5e223728094f1233ee2fc12dda (patch)
treec210ea1472b25519a62fd33e04e5809a16e76cbc /src
parentfd1d7be35f1b7f083071e246208498aa3b5ced3e (diff)
downloadmusl-c79cd27e9e81eb5e223728094f1233ee2fc12dda.tar.gz
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
Diffstat (limited to 'src')
-rw-r--r--src/prng/rand.c 6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/prng/rand.c b/src/prng/rand.c
index e3ce6347..c000cd24 100644
--- a/src/prng/rand.c
+++ b/src/prng/rand.c
@@ -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;
}
generated by cgit v1.2.1 (git 2.18.0) at 2025年10月08日 21:12:59 +0000

AltStyle によって変換されたページ (->オリジナル) /