/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright 2015-2019 Danny Robson */ #include "lcg.hpp" #include "../maths.hpp" using cruft::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 void lcg::discard (unsigned count) { while (count--) (*this)(); } /////////////////////////////////////////////////////////////////////////////// template struct cruft::rand::lcg; template struct cruft::rand::lcg;