Line data Source code
1 : // Copyright (c) 2021-2023 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef BITCOIN_TEST_LCG_H 6 : #define BITCOIN_TEST_LCG_H 7 : 8 : #include <cstdint> 9 : 10 : // Simple 32-bit linear congruential generator with 64-bit internal state, 11 : // often called as "MMIX by Donald Knuth". Knuth attributes the multiplier 12 : // to C. E. Haynes and the increment is not crucial (it only need be odd). 13 : // Knuth, Donald (1997). Seminumerical Algorithms Vol2. Sec 3.3.4. 3rd Ed. 14 : // 15 : // Low bits have short period, hence we use high bits which should have 16 : // the same period as the entire generator (2^64). 17 : class MMIXLinearCongruentialGenerator { 18 : uint64_t state; 19 : 20 : public: 21 2 : MMIXLinearCongruentialGenerator(uint64_t initialstate = 0) 22 2 : : state(initialstate) {} 23 : 24 : // pseudorandom, except the first value returned is the seed provided 25 : // (thus starting with 0, by default) 26 4096 : uint32_t next() { 27 4096 : uint32_t ret = state >> 32; 28 4096 : state = state * 6364136223846793005 + 1442695040888963407; 29 4096 : return ret; 30 : } 31 : }; 32 : 33 : #endif // BITCOIN_TEST_LCG_H