rand/lcg: add min/max/discard operations

This commit is contained in:
Danny Robson 2016-07-01 16:21:13 +10:00
parent 62482594bd
commit 325a7405b5
2 changed files with 36 additions and 1 deletions

View File

@ -56,6 +56,34 @@ lcg<T,M,A,C>::operator() (void)
}
///////////////////////////////////////////////////////////////////////////////
template <typename T, T M, T A, T C>
typename lcg<T,M,A,C>::result_type
lcg<T,M,A,C>::min (void)
{
return std::numeric_limits<result_type>::min ();
}
//-----------------------------------------------------------------------------
template <typename T, T M, T A, T C>
typename lcg<T,M,A,C>::result_type
lcg<T,M,A,C>::max (void)
{
return std::numeric_limits<result_type>::max ();
}
///////////////////////////////////////////////////////////////////////////////
template <typename T, T M, T A, T C>
void
lcg<T,M,A,C>::discard (unsigned count)
{
while (count--)
(*this)();
}
///////////////////////////////////////////////////////////////////////////////
template struct util::rand::lcg<uint32_t, util::pow2(31), 1103515245, 12345>;
template struct util::rand::lcg<uint64_t, 0ul, 6364136223846793005ul, 1ul>;

View File

@ -30,11 +30,18 @@ namespace util { namespace rand {
template <typename T, T M, T A, T C>
struct lcg {
public:
using result_type = T;
static_assert (std::is_unsigned<T>::value,
"LCG generates integer overflow which is undefined behaviour for signed types");
lcg (T seed);
T operator() (void);
result_type operator() (void);
static result_type min (void);
static result_type max (void);
void discard (unsigned);
private:
T m_x;