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 static volatile int lock[1];
27 volatile int *const __random_lockptr = lock;
29 static uint32_t lcg31(uint32_t x) {
30 return (1103515245*x + 12345) & 0x7fffffff;
33 static uint64_t lcg64(uint64_t x) {
34 return 6364136223846793005ull*x + 1;
37 static void *savestate() {
38 x[-1] = (n<<16)|(i<<8)|j;
42 static void loadstate(uint32_t *state) {
49 static void __srandom(unsigned seed) {
57 i = n == 31 || n == 7 ? 3 : 1;
59 for (k = 0; k < n; k++) {
63 /* make sure x contains at least one odd number */
67 void srandom(unsigned seed) {
73 char *initstate(unsigned seed, char *state, size_t size) {
90 x = (uint32_t*)state + 1;
97 char *setstate(char *state) {
102 loadstate((uint32_t*)state);
112 k = x[0] = lcg31(x[0]);