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 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_RAND_LCG_HPP
|
|
|
|
#define __UTIL_RAND_LCG_HPP
|
|
|
|
|
2016-10-11 23:47:57 +11:00
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
2016-02-03 12:13:03 +11:00
|
|
|
|
|
|
|
|
2018-08-05 14:42:02 +10: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:
|
2016-07-01 16:21:13 +10:00
|
|
|
using result_type = T;
|
|
|
|
|
2016-02-03 12:13:03 +11:00
|
|
|
static_assert (std::is_unsigned<T>::value,
|
|
|
|
"LCG generates integer overflow which is undefined behaviour for signed types");
|
2017-05-23 12:50:51 +10:00
|
|
|
explicit lcg (T seed);
|
2016-02-03 12:13:03 +11:00
|
|
|
|
2016-07-01 16:21:13 +10:00
|
|
|
result_type operator() (void);
|
|
|
|
|
|
|
|
static result_type min (void);
|
|
|
|
static result_type max (void);
|
|
|
|
|
|
|
|
void discard (unsigned);
|
2016-02-03 12:13:03 +11:00
|
|
|
|
|
|
|
private:
|
|
|
|
T m_x;
|
|
|
|
};
|
|
|
|
|
|
|
|
// glibc: typedef lcg<uint32_t, pow2(31), 1103515245, 12345> lcg_t;
|
|
|
|
using lcg_t = lcg<uint64_t,0u,6364136223846793005ul, 1ul>;
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2016-02-03 12:13:03 +11:00
|
|
|
|
|
|
|
#endif
|