/* Written in 2021 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 a Goresky-Klapper generalized multiply-with-carry generator
(see their paper "Efficient Multiply-with-Carry Random Number
Generators with Maximal Period", ACM Trans. Model. Comput. Simul.,
13(4), p. 310-321, 2003) with period approximately 2^127. While in
general slower than a scrambled linear generator, as it uses 128-bit
operations, it it is an excellent generator based on congruential
arithmetic.
As all MWC generators, it simulates a multiplicative LCG with
multiplier prime modulus m = 339698960761121441142761164663671108263
(0xff8fa3db04bb588e00000000adca32a7) and multiplier given by the
inverse of 2^64 modulo m. Note that the major difference with standard
(C)MWC generators is that the modulus has a more general form.
This additional freedom in the choice of the modulus fixes some
of the problems of (C)MWC generators, at the price of one
128-bit multiplication, one 64-bit multiplication, and one
128-bit sum. */
uint64_t x = 0, c = 1; /* The state can be seeded with any value, not all zeros. */
uint64_t inline next() {
const __uint128_t t = 0xff8fa3db04bb588e * (__uint128_t)x + c;
x = 0xd81fdde4eba3aae9 * (uint64_t)t;
c = (t + 0xadca32a7 * (__uint128_t)x) >> 64;
return x;
}