From 325a7405b537f1c8ee3f3c7edef6886e0a6dec0f Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 1 Jul 2016 16:21:13 +1000 Subject: [PATCH] rand/lcg: add min/max/discard operations --- rand/lcg.cpp | 28 ++++++++++++++++++++++++++++ rand/lcg.hpp | 9 ++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/rand/lcg.cpp b/rand/lcg.cpp index 31d9887c..37937407 100644 --- a/rand/lcg.cpp +++ b/rand/lcg.cpp @@ -56,6 +56,34 @@ lcg::operator() (void) } +/////////////////////////////////////////////////////////////////////////////// +template +typename lcg::result_type +lcg::min (void) +{ + return std::numeric_limits::min (); +} + + +//----------------------------------------------------------------------------- +template +typename lcg::result_type +lcg::max (void) +{ + return std::numeric_limits::max (); +} + + +/////////////////////////////////////////////////////////////////////////////// +template +void +lcg::discard (unsigned count) +{ + while (count--) + (*this)(); +} + + /////////////////////////////////////////////////////////////////////////////// template struct util::rand::lcg; template struct util::rand::lcg; diff --git a/rand/lcg.hpp b/rand/lcg.hpp index 8f2e74cc..e87ff243 100644 --- a/rand/lcg.hpp +++ b/rand/lcg.hpp @@ -30,11 +30,18 @@ namespace util { namespace rand { template struct lcg { public: + using result_type = T; + static_assert (std::is_unsigned::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;