diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/random/common_ops.h | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/random/common_ops.h')
-rw-r--r-- | util/random/common_ops.h | 198 |
1 files changed, 99 insertions, 99 deletions
diff --git a/util/random/common_ops.h b/util/random/common_ops.h index 602eede351..f9c50f12ac 100644 --- a/util/random/common_ops.h +++ b/util/random/common_ops.h @@ -1,130 +1,130 @@ -#pragma once - -#include <util/system/defaults.h> -#include <util/system/yassert.h> - -namespace NPrivate { +#pragma once + +#include <util/system/defaults.h> +#include <util/system/yassert.h> + +namespace NPrivate { constexpr double ToRandReal1(const ui32 x) noexcept { return x * (double)(1.0 / 4294967295.0); - } - + } + constexpr double ToRandReal2(const ui32 x) noexcept { return x * (double)(1.0 / 4294967296.0); - } - + } + constexpr double ToRandReal3(const ui32 x) noexcept { return ((double)x + 0.5) * (double)(1.0 / 4294967296.0); - } - + } + constexpr double ToRandReal1(const ui64 x) noexcept { return (x >> 11) * (double)(1.0 / 9007199254740991.0); - } - + } + constexpr double ToRandReal2(const ui64 x) noexcept { return (x >> 11) * (double)(1.0 / 9007199254740992.0); - } - + } + constexpr double ToRandReal3(const ui64 x) noexcept { return ((x >> 12) + 0.5) * (double)(1.0 / 4503599627370496.0); - } - + } + constexpr double ToRandReal4(const ui64 x) noexcept { return double(x * (double)(1.0 / 18446744073709551616.0L)); - } - - template <class T> + } + + template <class T> static inline ui64 ToRand64(T&& rng, ui32 x) noexcept { - return ((ui64)x) | (((ui64)rng.GenRand()) << 32); - } - - template <class T> + return ((ui64)x) | (((ui64)rng.GenRand()) << 32); + } + + template <class T> static constexpr ui64 ToRand64(T&&, ui64 x) noexcept { - return x; - } - - /* - * return value in range [0, max) from any generator - */ - template <class T, class TRandGen> - static T GenUniform(T max, TRandGen&& gen) { + return x; + } + + /* + * return value in range [0, max) from any generator + */ + template <class T, class TRandGen> + static T GenUniform(T max, TRandGen&& gen) { Y_VERIFY(max > 0, "Invalid random number range [0, 0)"); - - const T randmax = gen.RandMax() - gen.RandMax() % max; - T rand; - - while ((rand = gen.GenRand()) >= randmax) { - /* no-op */ - } - - return rand % max; - } -} - -template <class TRandType, class T> -struct TCommonRNG { - using TResult = TRandType; + + const T randmax = gen.RandMax() - gen.RandMax() % max; + T rand; + + while ((rand = gen.GenRand()) >= randmax) { + /* no-op */ + } + + return rand % max; + } +} + +template <class TRandType, class T> +struct TCommonRNG { + using TResult = TRandType; using result_type = TRandType; - + inline T& Engine() noexcept { - return static_cast<T&>(*this); - } - - static constexpr TResult _Min = TResult(0); - static constexpr TResult _Max = TResult(-1); - + return static_cast<T&>(*this); + } + + static constexpr TResult _Min = TResult(0); + static constexpr TResult _Max = TResult(-1); + static constexpr TResult RandMax() noexcept { - return _Max; - } - + return _Max; + } + static constexpr TResult RandMin() noexcept { - return _Min; - } - - /* generates uniformly distributed random number on [0, t) interval */ + return _Min; + } + + /* generates uniformly distributed random number on [0, t) interval */ inline TResult Uniform(TResult t) noexcept { - return ::NPrivate::GenUniform(t, Engine()); - } - - /* generates uniformly distributed random number on [f, t) interval */ + return ::NPrivate::GenUniform(t, Engine()); + } + + /* generates uniformly distributed random number on [f, t) interval */ inline TResult Uniform(TResult f, TResult t) noexcept { - return f + Uniform(t - f); - } - - /* generates 64-bit random number for current(may be 32 bit) rng */ + return f + Uniform(t - f); + } + + /* generates 64-bit random number for current(may be 32 bit) rng */ inline ui64 GenRand64() noexcept { - return ::NPrivate::ToRand64(Engine(), Engine().GenRand()); - } - - /* generates a random number on [0, 1]-real-interval */ + return ::NPrivate::ToRand64(Engine(), Engine().GenRand()); + } + + /* generates a random number on [0, 1]-real-interval */ inline double GenRandReal1() noexcept { - return ::NPrivate::ToRandReal1(Engine().GenRand()); - } - - /* generates a random number on [0, 1)-real-interval */ + return ::NPrivate::ToRandReal1(Engine().GenRand()); + } + + /* generates a random number on [0, 1)-real-interval */ inline double GenRandReal2() noexcept { - return ::NPrivate::ToRandReal2(Engine().GenRand()); - } - - /* generates a random number on (0, 1)-real-interval */ + return ::NPrivate::ToRandReal2(Engine().GenRand()); + } + + /* generates a random number on (0, 1)-real-interval */ inline double GenRandReal3() noexcept { - return ::NPrivate::ToRandReal3(Engine().GenRand()); - } - - /* generates a random number on [0, 1) with 53-bit resolution */ + return ::NPrivate::ToRandReal3(Engine().GenRand()); + } + + /* generates a random number on [0, 1) with 53-bit resolution */ inline double GenRandReal4() noexcept { - return ::NPrivate::ToRandReal4(Engine().GenRand64()); - } - - //compatibility stuff + return ::NPrivate::ToRandReal4(Engine().GenRand64()); + } + + //compatibility stuff inline TResult operator()() noexcept { - return Engine().GenRand(); - } - + return Engine().GenRand(); + } + static constexpr TResult max() noexcept { - return T::RandMax(); - } - + return T::RandMax(); + } + static constexpr TResult min() noexcept { - return T::RandMin(); - } -}; + return T::RandMin(); + } +}; |