]>
git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/prng/random.c
3326f09f0db8ad1fdaddb68639850522bf326a2e
7 this code uses the same lagged fibonacci generator as the
8 original bsd random implementation except for the seeding
9 which was broken in the original
12 static uint32_t init
[] = {
13 0x00000000,0x5851f42d,0xc0b18ccf,0xcbb5f646,
14 0xc7033129,0x30705b04,0x20fd5db4,0x9a8b7f78,
15 0x502959d8,0xab894868,0x6c0356a7,0x88cdb7ff,
16 0xb477d43f,0x70a3a52b,0xa8e4baf1,0xfd8341fc,
17 0x8ae16fd9,0x742d2f7a,0x0d1f0796,0x76035e09,
18 0x40f7702c,0x6fa72ca5,0xaaa84157,0x58a0df74,
19 0xc74a0364,0xae533cc4,0x04185faf,0x6de3b115,
20 0x0cab8628,0xf043bfa4,0x398150e9,0x37521657};
25 static uint32_t *x
= init
+1;
26 #ifdef __wasilibc_unmodified_upstream
27 static volatile int lock
[1];
28 volatile int *const __random_lockptr
= lock
;
31 static uint32_t lcg31(uint32_t x
) {
32 return (1103515245*x
+ 12345) & 0x7fffffff;
35 static uint64_t lcg64(uint64_t x
) {
36 return 6364136223846793005ull*x
+ 1;
39 static void *savestate() {
40 x
[-1] = (n
<<16)|(i
<<8)|j
;
44 static void loadstate(uint32_t *state
) {
51 static void __srandom(unsigned seed
) {
59 i
= n
== 31 || n
== 7 ? 3 : 1;
61 for (k
= 0; k
< n
; k
++) {
65 /* make sure x contains at least one odd number */
69 void srandom(unsigned seed
) {
75 char *initstate(unsigned seed
, char *state
, size_t size
) {
92 x
= (uint32_t*)state
+ 1;
99 char *setstate(char *state
) {
104 loadstate((uint32_t*)state
);
114 k
= x
[0] = lcg31(x
[0]);