diff options
Diffstat (limited to 'src/FastRandom.h')
-rw-r--r-- | src/FastRandom.h | 89 |
1 files changed, 34 insertions, 55 deletions
diff --git a/src/FastRandom.h b/src/FastRandom.h index 7ae5949d4..797eacb63 100644 --- a/src/FastRandom.h +++ b/src/FastRandom.h @@ -22,43 +22,37 @@ prefer calls to GetRandomProvider over creating new instances. namespace Detail { - /** Returns a low quality seed. */ - UInt32 GetRandomSeed(); - - /** Aliases true_type if Char is any variant of char ignoring signed-ness. */ - template <class Char> - using IsChar = typename std::is_same<typename std::make_signed<Char>::type, signed char>::type; - - template <class IntType> - struct cUniformImpl : - public std::conditional< - IsChar<IntType>::value, - typename std::conditional< // Match signed-ness of IntType - std::is_signed<IntType>::value, - std::uniform_int_distribution<short>, - std::uniform_int_distribution<unsigned short> - >::type, - std::uniform_int_distribution<IntType> - > - { - }; +/** Returns a low quality seed. */ +UInt32 GetRandomSeed(); + +/** Aliases true_type if Char is any variant of char ignoring signed-ness. */ +template <class Char> using IsChar = typename std::is_same<typename std::make_signed<Char>::type, signed char>::type; + +template <class IntType> +struct cUniformImpl : public std::conditional< + IsChar<IntType>::value, + typename std::conditional< // Match signed-ness of IntType + std::is_signed<IntType>::value, + std::uniform_int_distribution<short>, + std::uniform_int_distribution<unsigned short>>::type, + std::uniform_int_distribution<IntType>> +{ +}; - /** uniform_int_distribution<char> is undefined so this aliases a valid type. */ - template <class IntType> - using cUniform = typename cUniformImpl<IntType>::type; -} +/** uniform_int_distribution<char> is undefined so this aliases a valid type. */ +template <class IntType> using cUniform = typename cUniformImpl<IntType>::type; +} // namespace Detail /** Class to wrap any random engine to provide a more convenient interface. */ -template <class RandomEngine> -class cRandomWrapper +template <class RandomEngine> class cRandomWrapper { -public: + public: /** Initialize with a low quality seed. */ - cRandomWrapper(): + cRandomWrapper() : m_Engine(Detail::GetRandomSeed()) { } @@ -66,7 +60,7 @@ public: /** Initialize with a SeedSequence. */ template <class SeedSeq> - cRandomWrapper(SeedSeq & a_SeedSeq): + cRandomWrapper(SeedSeq & a_SeedSeq) : m_Engine(a_SeedSeq) { } @@ -74,18 +68,13 @@ public: /** Return a random IntType in the range [a_Min, a_Max]. */ - template <class IntType = int> - IntType RandInt(IntType a_Min, IntType a_Max) + template <class IntType = int> IntType RandInt(IntType a_Min, IntType a_Max) { ASSERT( - (a_Max >= a_Min) && - (a_Max <= std::numeric_limits<IntType>::max()) && + (a_Max >= a_Min) && (a_Max <= std::numeric_limits<IntType>::max()) && (a_Min >= std::numeric_limits<IntType>::min()) ); - Detail::cUniform<IntType> dist( - static_cast<IntType>(a_Min), - static_cast<IntType>(a_Max) - ); + Detail::cUniform<IntType> dist(static_cast<IntType>(a_Min), static_cast<IntType>(a_Max)); return static_cast<IntType>(dist(m_Engine)); } @@ -94,8 +83,7 @@ public: /** Return a random IntType in the range [0, a_Max]. */ - template <class IntType = int> - IntType RandInt(IntType a_Max) + template <class IntType = int> IntType RandInt(IntType a_Max) { ASSERT((a_Max >= 0) && (a_Max <= std::numeric_limits<IntType>::max())); Detail::cUniform<IntType> dist(IntType(0), static_cast<IntType>(a_Max)); @@ -107,8 +95,7 @@ public: /** Return a random IntType in the range [0, std::numeric_limits<IntType>::max()]. */ - template <class IntType = int> - IntType RandInt() + template <class IntType = int> IntType RandInt() { Detail::cUniform<IntType> dist(IntType(0), std::numeric_limits<IntType>::max()); return static_cast<IntType>(dist(m_Engine)); @@ -119,8 +106,7 @@ public: /** Return a random RealType in the range [a_Min, a_Max). */ - template <class RealType = float> - RealType RandReal(RealType a_Min, RealType a_Max) + template <class RealType = float> RealType RandReal(RealType a_Min, RealType a_Max) { std::uniform_real_distribution<RealType> dist(a_Min, a_Max); return dist(m_Engine); @@ -131,8 +117,7 @@ public: /** Return a random RealType in the range [0, a_Max). */ - template <class RealType = float> - RealType RandReal(RealType a_Max) + template <class RealType = float> RealType RandReal(RealType a_Max) { std::uniform_real_distribution<RealType> dist(RealType(0), a_Max); return dist(m_Engine); @@ -143,8 +128,7 @@ public: /** Return a random RealType in the range [0, 1). */ - template <class RealType = float> - RealType RandReal() + template <class RealType = float> RealType RandReal() { std::uniform_real_distribution<RealType> dist; return dist(m_Engine); @@ -165,13 +149,9 @@ public: /** Returns a reference to the underlying random engine. */ - RandomEngine & Engine() - { - return m_Engine; - } - -private: + RandomEngine & Engine() { return m_Engine; } + private: RandomEngine m_Engine; }; @@ -184,8 +164,7 @@ struct cRandomDeviceSeeder { using result_type = std::random_device::result_type; - template <class Itr> - void generate(Itr first, Itr last) + template <class Itr> void generate(Itr first, Itr last) { std::random_device rd; std::uniform_int_distribution<result_type> dist; |