2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2011-10-18 13:28:32 +00:00
|
|
|
#ifndef SPROUT_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
|
|
|
|
#define SPROUT_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <ios>
|
2012-11-15 09:36:55 +00:00
|
|
|
#include <type_traits>
|
2011-10-18 13:28:32 +00:00
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2011-10-18 13:28:32 +00:00
|
|
|
#include <sprout/random/detail/const_mod.hpp>
|
2013-11-09 08:39:57 +00:00
|
|
|
#include <sprout/random/detail/seed_impl.hpp>
|
2011-10-18 13:28:32 +00:00
|
|
|
#include <sprout/random/random_result.hpp>
|
2013-11-10 11:50:16 +00:00
|
|
|
#include <sprout/random/type_traits.hpp>
|
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2013-10-05 04:35:26 +00:00
|
|
|
#include <sprout/math/greater_equal.hpp>
|
2013-03-18 10:12:21 +00:00
|
|
|
#include <sprout/assert.hpp>
|
2011-10-18 13:28:32 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace random {
|
|
|
|
//
|
|
|
|
// inversive_congruential_engine
|
|
|
|
//
|
|
|
|
template<typename IntType, IntType a, IntType b, IntType p>
|
|
|
|
class inversive_congruential_engine {
|
|
|
|
public:
|
|
|
|
typedef IntType result_type;
|
|
|
|
private:
|
2013-08-09 13:14:43 +00:00
|
|
|
struct private_construct_t {};
|
2011-10-18 13:28:32 +00:00
|
|
|
public:
|
2013-11-09 08:39:57 +00:00
|
|
|
SPROUT_STATIC_CONSTEXPR result_type multiplier = a;
|
|
|
|
SPROUT_STATIC_CONSTEXPR result_type increment = b;
|
|
|
|
SPROUT_STATIC_CONSTEXPR result_type modulus = p;
|
|
|
|
SPROUT_STATIC_CONSTEXPR result_type default_seed = 1;
|
2012-11-16 04:40:19 +00:00
|
|
|
public:
|
|
|
|
static SPROUT_CONSTEXPR result_type static_min() SPROUT_NOEXCEPT {
|
2013-11-09 08:39:57 +00:00
|
|
|
return increment == 0 ? 1 : 0;
|
2011-10-18 13:28:32 +00:00
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
static SPROUT_CONSTEXPR result_type static_max() SPROUT_NOEXCEPT {
|
2011-10-18 13:28:32 +00:00
|
|
|
return modulus - 1;
|
|
|
|
}
|
2013-11-09 08:39:57 +00:00
|
|
|
static SPROUT_CONSTEXPR result_type init_seed_3(result_type x0) {
|
2013-03-18 10:12:21 +00:00
|
|
|
return SPROUT_ASSERT(sprout::math::greater_equal(x0, static_min())), SPROUT_ASSERT(x0 <= static_max()), x0;
|
2011-10-18 13:28:32 +00:00
|
|
|
}
|
2013-11-09 08:39:57 +00:00
|
|
|
static SPROUT_CONSTEXPR result_type init_seed_2(result_type x0) {
|
2013-03-18 10:12:21 +00:00
|
|
|
return init_seed_3(increment == 0 && x0 == 0 ? 1 : x0);
|
2011-10-18 13:28:32 +00:00
|
|
|
}
|
2013-11-09 08:39:57 +00:00
|
|
|
static SPROUT_CONSTEXPR result_type init_seed_1(result_type x0) {
|
2011-10-18 13:28:32 +00:00
|
|
|
return init_seed_2(x0 <= 0 && x0 != 0 ? x0 + modulus : x0);
|
|
|
|
}
|
2013-11-09 08:39:57 +00:00
|
|
|
static SPROUT_CONSTEXPR result_type init_seed(result_type x0 = default_seed) {
|
2011-10-18 13:28:32 +00:00
|
|
|
return init_seed_1(modulus == 0 ? x0 : x0 % modulus);
|
|
|
|
}
|
2013-11-10 11:50:16 +00:00
|
|
|
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
|
2013-11-09 08:39:57 +00:00
|
|
|
static SPROUT_CXX14_CONSTEXPR result_type init_seed(Sseq& seq) {
|
|
|
|
return init_seed(sprout::random::detail::seed_one_int<result_type, modulus>(seq));
|
|
|
|
}
|
2013-11-10 11:50:16 +00:00
|
|
|
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
|
2013-11-09 08:39:57 +00:00
|
|
|
static SPROUT_CONSTEXPR result_type init_seed(Sseq const& seq) {
|
|
|
|
return init_seed(sprout::random::detail::seed_one_int<result_type, modulus>(seq));
|
|
|
|
}
|
|
|
|
template<typename InputIterator>
|
|
|
|
static SPROUT_CONSTEXPR result_type init_seed(InputIterator first, InputIterator last) {
|
|
|
|
return init_seed(sprout::random::detail::get_one_int<result_type, modulus>(first, last));
|
|
|
|
}
|
2011-10-18 13:28:32 +00:00
|
|
|
private:
|
2013-11-09 08:39:57 +00:00
|
|
|
result_type x_;
|
2011-10-18 13:28:32 +00:00
|
|
|
private:
|
2013-11-09 08:39:57 +00:00
|
|
|
SPROUT_CONSTEXPR inversive_congruential_engine(result_type x, private_construct_t)
|
2011-10-18 13:28:32 +00:00
|
|
|
: x_(x)
|
|
|
|
{}
|
2012-11-14 02:22:05 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<inversive_congruential_engine>
|
|
|
|
generate(result_type result) const {
|
2011-10-18 13:28:32 +00:00
|
|
|
return sprout::random::random_result<inversive_congruential_engine>(
|
|
|
|
result,
|
2013-08-09 13:14:43 +00:00
|
|
|
inversive_congruential_engine(result, private_construct_t())
|
2011-10-18 13:28:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR inversive_congruential_engine()
|
|
|
|
: x_(init_seed(default_seed))
|
|
|
|
{}
|
2013-11-09 08:39:57 +00:00
|
|
|
explicit SPROUT_CONSTEXPR inversive_congruential_engine(result_type x0)
|
2011-10-18 13:28:32 +00:00
|
|
|
: x_(init_seed(x0))
|
|
|
|
{}
|
2013-11-10 11:50:16 +00:00
|
|
|
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
|
2013-11-09 08:39:57 +00:00
|
|
|
explicit SPROUT_CXX14_CONSTEXPR inversive_congruential_engine(Sseq& seq)
|
|
|
|
: x_(init_seed(seq))
|
|
|
|
{}
|
2013-11-10 11:50:16 +00:00
|
|
|
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
|
2013-11-09 08:39:57 +00:00
|
|
|
explicit SPROUT_CONSTEXPR inversive_congruential_engine(Sseq const& seq)
|
|
|
|
: x_(init_seed(seq))
|
|
|
|
{}
|
|
|
|
template<typename InputIterator>
|
|
|
|
SPROUT_CONSTEXPR inversive_congruential_engine(InputIterator first, InputIterator last)
|
|
|
|
: x_(init_seed(first, last))
|
|
|
|
{}
|
|
|
|
SPROUT_CXX14_CONSTEXPR void seed(result_type x0 = default_seed) {
|
|
|
|
x_ = init_seed(x0);
|
|
|
|
}
|
2013-11-10 11:50:16 +00:00
|
|
|
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
|
2013-11-09 08:39:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void seed(Sseq& seq) {
|
|
|
|
x_ = init_seed(seq);
|
|
|
|
}
|
2013-11-10 11:50:16 +00:00
|
|
|
template<typename Sseq, typename sprout::enabler_if<sprout::random::is_seed_seq<Sseq>::value>::type = sprout::enabler>
|
2013-11-09 08:39:57 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void seed(Sseq const& seq) {
|
|
|
|
x_ = init_seed(seq);
|
|
|
|
}
|
|
|
|
template<typename InputIterator>
|
|
|
|
SPROUT_CXX14_CONSTEXPR void seed(InputIterator first, InputIterator last) {
|
|
|
|
x_ = init_seed(first, last);
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
|
2011-10-18 13:28:32 +00:00
|
|
|
return static_min();
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
|
2011-10-18 13:28:32 +00:00
|
|
|
return static_max();
|
|
|
|
}
|
2013-11-05 08:39:11 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR result_type operator()() {
|
2013-11-09 08:39:57 +00:00
|
|
|
typedef sprout::random::detail::const_mod<result_type, p> do_mod;
|
2013-11-05 08:39:11 +00:00
|
|
|
x_ = do_mod::mult_add(a, do_mod::invert(x_), b);
|
|
|
|
return x_;
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<inversive_congruential_engine> const operator()() const {
|
2013-11-09 08:39:57 +00:00
|
|
|
typedef sprout::random::detail::const_mod<result_type, p> do_mod;
|
2011-10-18 13:28:32 +00:00
|
|
|
return generate(do_mod::mult_add(a, do_mod::invert(x_), b));
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator==(inversive_congruential_engine const& lhs, inversive_congruential_engine const& rhs) SPROUT_NOEXCEPT {
|
2011-10-18 13:28:32 +00:00
|
|
|
return lhs.x_ == rhs.x_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator!=(inversive_congruential_engine const& lhs, inversive_congruential_engine const& rhs) SPROUT_NOEXCEPT {
|
2011-10-18 13:28:32 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
template<typename Elem, typename Traits>
|
2013-11-02 09:28:18 +00:00
|
|
|
friend SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>& operator>>(
|
2011-10-18 13:28:32 +00:00
|
|
|
std::basic_istream<Elem, Traits>& lhs,
|
|
|
|
inversive_congruential_engine& rhs
|
|
|
|
)
|
|
|
|
{
|
2013-11-09 08:39:57 +00:00
|
|
|
result_type x;
|
2011-10-18 13:28:32 +00:00
|
|
|
if (lhs >> x) {
|
2013-03-18 10:12:21 +00:00
|
|
|
if (sprout::math::greater_equal(x, static_min()) && x <= static_max()) {
|
2011-10-18 13:28:32 +00:00
|
|
|
rhs.x_ = x;
|
|
|
|
} else {
|
|
|
|
lhs.setstate(std::ios_base::failbit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
template<typename Elem, typename Traits>
|
2013-11-02 09:28:18 +00:00
|
|
|
friend SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>& operator<<(
|
2011-10-18 13:28:32 +00:00
|
|
|
std::basic_ostream<Elem, Traits>& lhs,
|
|
|
|
inversive_congruential_engine const& rhs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return lhs << rhs.x_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename IntType, IntType a, IntType b, IntType p>
|
2012-06-15 15:08:42 +00:00
|
|
|
SPROUT_CONSTEXPR_OR_CONST IntType sprout::random::inversive_congruential_engine<IntType, a, b, p>::multiplier;
|
2011-10-18 13:28:32 +00:00
|
|
|
template<typename IntType, IntType a, IntType b, IntType p>
|
2012-06-15 15:08:42 +00:00
|
|
|
SPROUT_CONSTEXPR_OR_CONST IntType sprout::random::inversive_congruential_engine<IntType, a, b, p>::increment;
|
2011-10-18 13:28:32 +00:00
|
|
|
template<typename IntType, IntType a, IntType b, IntType p>
|
2012-06-15 15:08:42 +00:00
|
|
|
SPROUT_CONSTEXPR_OR_CONST IntType sprout::random::inversive_congruential_engine<IntType, a, b, p>::modulus;
|
2011-10-18 13:28:32 +00:00
|
|
|
template<typename IntType, IntType a, IntType b, IntType p>
|
2012-06-15 15:08:42 +00:00
|
|
|
SPROUT_CONSTEXPR_OR_CONST IntType sprout::random::inversive_congruential_engine<IntType, a, b, p>::default_seed;
|
2011-10-18 13:28:32 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// hellekalek1995
|
|
|
|
//
|
2013-11-09 08:39:57 +00:00
|
|
|
typedef sprout::random::inversive_congruential_engine<std::uint_fast32_t, 9102, 2147483647 - 36884165, 2147483647> hellekalek1995;
|
2013-03-22 05:24:19 +00:00
|
|
|
} // namespace random
|
2011-10-18 13:28:32 +00:00
|
|
|
|
2012-04-14 03:31:47 +00:00
|
|
|
using sprout::random::inversive_congruential_engine;
|
2011-10-18 13:28:32 +00:00
|
|
|
using sprout::random::hellekalek1995;
|
2013-03-22 05:24:19 +00:00
|
|
|
} // namespace sprout
|
2011-10-18 13:28:32 +00:00
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
#endif // #ifndef SPROUT_RANDOM_INVERSIVE_CONGRUENTIAL_HPP
|