libcruft-util/cruft/util/rand/lcg.hpp

46 lines
1.1 KiB
C++
Raw Normal View History

2016-02-03 12:13:03 +11:00
/*
2018-08-04 15:14:06 +10:00
* 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/.
2016-02-03 12:13:03 +11:00
*
* Copyright 2015-2019 Danny Robson <danny@nerdcruft.net>
2016-02-03 12:13:03 +11:00
*/
#pragma once
#include "../std.hpp"
2016-02-03 12:13:03 +11:00
#include <type_traits>
#include <limits>
2016-02-03 12:13:03 +11:00
namespace cruft::rand {
2016-02-03 12:13:03 +11:00
/// linear congruential generator
///
/// T: output/state type
/// M: modulus
/// A: multiplier
/// C: increment
template <typename T, T M, T A, T C>
struct lcg {
public:
using result_type = T;
static_assert (std::is_unsigned_v<T>);
2017-05-23 12:50:51 +10:00
explicit lcg (T seed);
2016-02-03 12:13:03 +11:00
result_type operator() (void);
static constexpr auto min (void) { return std::numeric_limits<T>::min (); }
static constexpr auto max (void) { return std::numeric_limits<T>::max (); }
void discard (unsigned);
2016-02-03 12:13:03 +11:00
private:
T m_x;
};
// glibc: typedef lcg<u64, pow2(31), 1103515245, 12345> lcg_t;
using lcg_t = lcg<u64,0u,6364136223846793005ul, 1ul>;
2017-01-05 15:06:49 +11:00
}