/* Written in 2015 by Sebastiano Vigna (vigna@acm.org)
To the extent possible under law, the author has dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
See . */
#include
/* This is probably the oldest known PRNG: a Lehmer generator (a.k.a.
multiplicative LCG). It simply multiplies the current state by a
constant. It is a very fast generator passing BigCrush, albeit slower
than xoroshiro128+, and with a smaller period (2^126). It needs support
for 128-bit integers from the compiler. The less significant bit,
moreover, has shorter period 2^62, the next bit 2^63, etc.
Note, however, that as 128-bit processor and compilers will become
common this generator will likely become extremely fast.
The multiplier has been taken from
Pierre L'Ecuyer. Tables of linear congruential generators of different
sizes and good lattice structure. Math. Comput. 68, 225 (1999), 249-260.
IMPORTANT: On big-endian architectures, the role of s[0] and s[1] must
be exchanged.
*/
#include
#define MULTIPLIER ((__uint128)0x12e15e35b500f16e << 64 | 0x2e714eb2b37916a5)
/* The state must be seeded with two 64-bit values, among which s[0] MUST be odd. */
static union {
__int128 x;
uint64_t s[2];
} state;
uint64_t next(void) {
state.x *= MULTIPLIER;
return state.s[1];
}