diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/random/mersenne64.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/random/mersenne64.h')
-rw-r--r-- | util/random/mersenne64.h | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/util/random/mersenne64.h b/util/random/mersenne64.h new file mode 100644 index 0000000000..12ca43b6af --- /dev/null +++ b/util/random/mersenne64.h @@ -0,0 +1,50 @@ +#pragma once + +#include <util/system/defaults.h> + +class IInputStream; + +namespace NPrivate { + class TMersenne64 { + static constexpr int NN = 312; + + public: + inline TMersenne64(ui64 s = ULL(19650218)) + : mti(NN + 1) + { + InitGenRand(s); + } + + inline TMersenne64(const ui64* keys, size_t len) noexcept + : mti(NN + 1) + { + InitByArray(keys, len); + } + + TMersenne64(IInputStream& input); + + inline ui64 GenRand() noexcept { + if (mti >= NN) { + InitNext(); + } + + ui64 x = mt[mti++]; + + x ^= (x >> 29) & ULL(0x5555555555555555); + x ^= (x << 17) & ULL(0x71D67FFFEDA60000); + x ^= (x << 37) & ULL(0xFFF7EEE000000000); + x ^= (x >> 43); + + return x; + } + + private: + void InitNext() noexcept; + void InitGenRand(ui64 seed) noexcept; + void InitByArray(const ui64* init_key, size_t key_length) noexcept; + + private: + ui64 mt[NN]; + int mti; + }; +} |