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/random.cpp | |
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/random.cpp')
-rw-r--r-- | util/random/random.cpp | 194 |
1 files changed, 97 insertions, 97 deletions
diff --git a/util/random/random.cpp b/util/random/random.cpp index 71f9323856..561f8aa412 100644 --- a/util/random/random.cpp +++ b/util/random/random.cpp @@ -1,118 +1,118 @@ -#include "random.h" -#include "entropy.h" -#include "mersenne.h" - -#include <util/system/getpid.h> -#include <util/thread/singleton.h> -#include <util/stream/multi.h> -#include <util/stream/mem.h> -#include <util/digest/numeric.h> - -namespace { - struct TProcStream { - ui32 Extra; - TMemoryInput MI; - TMultiInput TI; - - static inline ui32 ExtraData() noexcept { - ui32 data; - - EntropyPool().LoadOrFail(&data, sizeof(data)); - - return IntHash(data ^ GetPID()); - } - - inline TProcStream() noexcept - : Extra(ExtraData()) - , MI(&Extra, sizeof(Extra)) - , TI(&MI, &EntropyPool()) - { - } - - inline IInputStream& S() noexcept { - return TI; - } - }; - - template <class T> - struct TRndGen: public TMersenne<T> { - inline TRndGen() - : TMersenne<T>(TProcStream().S()) - { - } +#include "random.h" +#include "entropy.h" +#include "mersenne.h" + +#include <util/system/getpid.h> +#include <util/thread/singleton.h> +#include <util/stream/multi.h> +#include <util/stream/mem.h> +#include <util/digest/numeric.h> + +namespace { + struct TProcStream { + ui32 Extra; + TMemoryInput MI; + TMultiInput TI; + + static inline ui32 ExtraData() noexcept { + ui32 data; + + EntropyPool().LoadOrFail(&data, sizeof(data)); + + return IntHash(data ^ GetPID()); + } + + inline TProcStream() noexcept + : Extra(ExtraData()) + , MI(&Extra, sizeof(Extra)) + , TI(&MI, &EntropyPool()) + { + } + + inline IInputStream& S() noexcept { + return TI; + } + }; + + template <class T> + struct TRndGen: public TMersenne<T> { + inline TRndGen() + : TMersenne<T>(TProcStream().S()) + { + } inline TRndGen(T seed) : TMersenne<T>(seed) { } - }; - - template <class T> - static inline TRndGen<T>* GetRndGen() { + }; + + template <class T> + static inline TRndGen<T>* GetRndGen() { return FastTlsSingletonWithPriority<TRndGen<T>, 2>(); - } - - template <unsigned N> - struct TToRealTypeBySize { + } + + template <unsigned N> + struct TToRealTypeBySize { using TResult = ui32; - }; - - template <> - struct TToRealTypeBySize<8> { + }; + + template <> + struct TToRealTypeBySize<8> { using TResult = ui64; - }; - - template <class T> - struct TToRealType { + }; + + template <class T> + struct TToRealType { using TResult = typename TToRealTypeBySize<sizeof(T)>::TResult; - }; -} - -#define DEF_RND(TY) \ - template <> \ - TY RandomNumber<TY>() { \ - return GetRndGen<TToRealType<TY>::TResult>()->GenRand(); \ - } \ - \ - template <> \ - TY RandomNumber<TY>(TY n) { \ - return GetRndGen<TToRealType<TY>::TResult>()->Uniform(n); \ - } - + }; +} + +#define DEF_RND(TY) \ + template <> \ + TY RandomNumber<TY>() { \ + return GetRndGen<TToRealType<TY>::TResult>()->GenRand(); \ + } \ + \ + template <> \ + TY RandomNumber<TY>(TY n) { \ + return GetRndGen<TToRealType<TY>::TResult>()->Uniform(n); \ + } + DEF_RND(char) -DEF_RND(unsigned char) -DEF_RND(unsigned int) -DEF_RND(unsigned long) -DEF_RND(unsigned short) -DEF_RND(unsigned long long) - +DEF_RND(unsigned char) +DEF_RND(unsigned int) +DEF_RND(unsigned long) +DEF_RND(unsigned short) +DEF_RND(unsigned long long) + #undef DEF_RND -template <> +template <> bool RandomNumber<bool>() { return RandomNumber<ui8>() % 2 == 0; } template <> -float RandomNumber<float>() { - float ret; - - do { - ret = (float)GetRndGen<ui64>()->GenRandReal2(); +float RandomNumber<float>() { + float ret; + + do { + ret = (float)GetRndGen<ui64>()->GenRandReal2(); } while (ret >= 1); - - return ret; -} - -template <> -double RandomNumber<double>() { - return GetRndGen<ui64>()->GenRandReal4(); -} - -template <> -long double RandomNumber<long double>() { - return RandomNumber<double>(); -} + + return ret; +} + +template <> +double RandomNumber<double>() { + return GetRndGen<ui64>()->GenRandReal4(); +} + +template <> +long double RandomNumber<long double>() { + return RandomNumber<double>(); +} void ResetRandomState() { *GetRndGen<ui32>() = TRndGen<ui32>(); |