initial commit of prng implementation by Szabolcs Nagy
[musl] / src / prng / random.c
1 /*
2  * random.c - Copyright © 2011 Szabolcs Nagy
3  * Permission to use, copy, modify, and/or distribute this code
4  * for any purpose with or without fee is hereby granted.
5  * There is no warranty.
6 */
7
8 #include <stdlib.h>
9 #include <stdint.h>
10
11 /*
12 this code uses the same lagged fibonacci generator as the
13 original bsd random implementation except for the seeding
14
15 different seeds produce different sequences with long period
16 (other libcs seed the state with a park-miller generator
17 when seed=0 some fail to produce good random sequence
18 others produce the same sequence as another seed)
19 */
20
21 static uint32_t init[] = {
22 0x00000000,0x5851f42d,0xc0b18ccf,0xcbb5f646,
23 0xc7033129,0x30705b04,0x20fd5db4,0x9a8b7f78,
24 0x502959d8,0xab894868,0x6c0356a7,0x88cdb7ff,
25 0xb477d43f,0x70a3a52b,0xa8e4baf1,0xfd8341fc,
26 0x8ae16fd9,0x742d2f7a,0x0d1f0796,0x76035e09,
27 0x40f7702c,0x6fa72ca5,0xaaa84157,0x58a0df74,
28 0xc74a0364,0xae533cc4,0x04185faf,0x6de3b115,
29 0x0cab8628,0xf043bfa4,0x398150e9,0x37521657};
30
31 static int n = 31;
32 static int i = 3;
33 static int j = 0;
34 static uint32_t *x = init+1;
35
36 static uint32_t lcg31(uint32_t x) {
37         return (1103515245*x + 12345) & 0x7fffffff;
38 }
39
40 static uint64_t lcg64(uint64_t x) {
41         return 6364136223846793005ull*x + 1;
42 }
43
44 static void *savestate() {
45         x[-1] = (n<<16)|(i<<8)|j;
46         return x-1;
47 }
48
49 static void loadstate(uint32_t *state) {
50         x = state+1;
51         n = x[-1]>>16;
52         i = (x[-1]>>8)&0xff;
53         j = x[-1]&0xff;
54 }
55
56 void srandom(unsigned seed) {
57         int k;
58         uint64_t s = seed;
59
60         if (n == 0) {
61                 x[0] = s;
62                 return;
63         }
64         i = n == 31 || n == 7 ? 3 : 1;
65         j = 0;
66         for (k = 0; k < n; k++) {
67                 s = lcg64(s);
68                 x[k] = s>>32;
69         }
70         /* make sure x contains at least one odd number */
71         x[0] |= 1;
72 }
73
74 char *initstate(unsigned seed, char *state, size_t size) {
75         void *old = savestate();
76         if (size < 8)
77                 return 0;
78         else if (size < 32)
79                 n = 0;
80         else if (size < 64)
81                 n = 7;
82         else if (size < 128)
83                 n = 15;
84         else if (size < 256)
85                 n = 31;
86         else
87                 n = 63;
88         x = (uint32_t*)state + 1;
89         srandom(seed);
90         return old;
91 }
92
93 char *setstate(char *state) {
94         void *old = savestate();
95         loadstate((uint32_t*)state);
96         return old;
97 }
98
99 long random(void) {
100         long k;
101
102         if (n == 0)
103                 return x[0] = lcg31(x[0]);
104         x[i] += x[j];
105         k = x[i]>>1;
106         if (++i == n)
107                 i = 0;
108         if (++j == n)
109                 j = 0;
110         return k;
111 }