/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Copyright 2015 Danny Robson */ #include "lcg.hpp" #include "../maths.hpp" using util::rand::lcg; /////////////////////////////////////////////////////////////////////////////// template static constexpr bool is_coprime (T M, T C) { if (M == 0) return true; if (std::gcd (M, C) == 1u) return true; return false; } /////////////////////////////////////////////////////////////////////////////// template lcg::lcg (T seed): m_x (seed) { // ensure this assertion isn't in a header, it could be pretty expensive // to evaluate often. static_assert (is_coprime (M, C), "multiplier and increment must be coprime"); } /////////////////////////////////////////////////////////////////////////////// template T lcg::operator() (void) { m_x = (A * m_x + C); if (M != 0) m_x %= M; return m_x; } /////////////////////////////////////////////////////////////////////////////// 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;