diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /util/random/normal.h | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'util/random/normal.h')
-rw-r--r-- | util/random/normal.h | 76 |
1 files changed, 38 insertions, 38 deletions
diff --git a/util/random/normal.h b/util/random/normal.h index 4b6d0e3b8e..1ed132b532 100644 --- a/util/random/normal.h +++ b/util/random/normal.h @@ -1,38 +1,38 @@ -#pragma once - -#include <cmath> - -// sometimes we need stateless normal distribution... - -/* - * normal distribution with Box-Muller transform - * http://www.design.caltech.edu/erik/Misc/Gaussian.html - */ -template <typename T, typename TRng> -static inline T StdNormalDistribution(TRng&& rng) noexcept { - T x; - T y; - T r; - - do { - x = (T)rng.GenRandReal1() * T(2) - T(1); - y = (T)rng.GenRandReal1() * T(2) - T(1); - r = x * x + y * y; - } while (r > T(1) || r <= T(0)); - - return x * std::sqrt(-T(2) * std::log(r) / r); -} - -template <typename T, typename TRng> -static inline T NormalDistribution(TRng&& rng, T m, T d) noexcept { - return StdNormalDistribution<T>(rng) * d + m; -} - -// specialized for float, double, long double -template <class T> -T StdNormalRandom() noexcept; - -template <class T> -static inline T NormalRandom(T m, T d) noexcept { - return StdNormalRandom<T>() * d + m; -} +#pragma once + +#include <cmath> + +// sometimes we need stateless normal distribution... + +/* + * normal distribution with Box-Muller transform + * http://www.design.caltech.edu/erik/Misc/Gaussian.html + */ +template <typename T, typename TRng> +static inline T StdNormalDistribution(TRng&& rng) noexcept { + T x; + T y; + T r; + + do { + x = (T)rng.GenRandReal1() * T(2) - T(1); + y = (T)rng.GenRandReal1() * T(2) - T(1); + r = x * x + y * y; + } while (r > T(1) || r <= T(0)); + + return x * std::sqrt(-T(2) * std::log(r) / r); +} + +template <typename T, typename TRng> +static inline T NormalDistribution(TRng&& rng, T m, T d) noexcept { + return StdNormalDistribution<T>(rng) * d + m; +} + +// specialized for float, double, long double +template <class T> +T StdNormalRandom() noexcept; + +template <class T> +static inline T NormalRandom(T m, T d) noexcept { + return StdNormalRandom<T>() * d + m; +} |