| 1 | /* Portable pseudorandom number generator |
| 2 | * Based on a 24,55 Fibonacci generator, using 55/503 rejection |
| 3 | * c.f.- TAoCP, 3.2.2(7), for j=24,k=55,m=2^64 |
| 4 | * |
| 5 | * THIS FILE IS PUBLIC DOMAIN CODE. |
| 6 | * |
| 7 | * Written by Bob Adolf. |
| 8 | * Attribution is appreciated but by no means legally required. |
| 9 | * |
| 10 | * This function is sufficient for most non-crypto applications. |
| 11 | * It passes all but one of George Marsaglia's "diehard" randomness tests. |
| 12 | * (overlapping 5-tuple permutations, which is allegedly buggy) |
| 13 | */ |
| 14 | |
| 15 | #ifndef __PRNG_H__ |
| 16 | #define __PRNG_H__ |
| 17 | |
| 18 | #include <inttypes.h> |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | #define LAG1 (UINT16_C(24)) |
| 24 | #define LAG2 (UINT16_C(55)) |
| 25 | #define RAND_SSIZE ((UINT16_C(1)) << 6) |
| 26 | #define RAND_SMASK (RAND_SSIZE - 1) |
| 27 | #define RAND_EXHAUST_LIMIT LAG2 |
| 28 | // 10x is a heuristic, it just needs to be large enough to remove correlation |
| 29 | #define RAND_REFILL_COUNT ((LAG2 * 10) - RAND_EXHAUST_LIMIT) |
| 30 | struct prng_rand_t |
| 31 | { |
| 32 | uint64_t s[RAND_SSIZE]; // Lags |
| 33 | uint_fast16_t i; // Location of the current lag |
| 34 | uint_fast16_t c; // Exhaustion count |
| 35 | }; |
| 36 | |
| 37 | #define PRNG_RAND_MAX UINT64_MAX |
| 38 | |
| 39 | static inline uint64_t prng_rand(struct prng_rand_t* state) |
| 40 | { |
| 41 | uint_fast16_t i; |
| 42 | uint_fast16_t r, new_rands = 0; |
| 43 | |
| 44 | if (!state->c) |
| 45 | { // Randomness exhausted, run forward to refill |
| 46 | new_rands += RAND_REFILL_COUNT + 1; |
| 47 | state->c = RAND_EXHAUST_LIMIT - 1; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | new_rands = 1; |
| 52 | state->c--; |
| 53 | } |
| 54 | |
| 55 | for (r = 0; r < new_rands; r++) |
| 56 | { |
| 57 | i = state->i; |
| 58 | state->s[i & RAND_SMASK] = state->s[(i + RAND_SSIZE - LAG1) & RAND_SMASK] |
| 59 | + state->s[(i + RAND_SSIZE - LAG2) & RAND_SMASK]; |
| 60 | state->i++; |
| 61 | } |
| 62 | return state->s[i & RAND_SMASK]; |
| 63 | } |
| 64 | |
| 65 | static inline void prng_srand(uint64_t seed, struct prng_rand_t* state) |
| 66 | { |
| 67 | uint_fast16_t i; |
| 68 | // Naive seed |
| 69 | state->c = RAND_EXHAUST_LIMIT; |
| 70 | state->i = 0; |
| 71 | |
| 72 | state->s[0] = seed; |
| 73 | for (i = 1; i < RAND_SSIZE; i++) |
| 74 | { |
| 75 | // Arbitrary magic, mostly to eliminate the effect of low-value seeds. |
| 76 | // Probably could be better, but the run-up obviates any real need to. |
| 77 | state->s[i] = i * (UINT64_C(2147483647)) + seed; |
| 78 | } |
| 79 | |
| 80 | // Run forward 10,000 numbers |
| 81 | for (i = 0; i < 10000; i++) |
| 82 | { |
| 83 | prng_rand(state); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Clean up our macros |
| 88 | #undef LAG1 |
| 89 | #undef LAG2 |
| 90 | #undef RAND_SSIZE |
| 91 | #undef RAND_SMASK |
| 92 | #undef RAND_EXHAUST_LIMIT |
| 93 | #undef RAND_REFILL_COUNT |
| 94 | |
| 95 | // PRNG_RAND_MAX is exported |
| 96 | |
| 97 | #endif |