mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
sprout/random/taus88.hpp 追加
This commit is contained in:
parent
ae589d7fc0
commit
8ebe6ea878
5 changed files with 236 additions and 1 deletions
|
@ -5,6 +5,9 @@
|
|||
#include <sprout/random/linear_congruential.hpp>
|
||||
#include <sprout/random/additive_combine.hpp>
|
||||
#include <sprout/random/shuffle_order.hpp>
|
||||
#include <sprout/random/linear_feedback_shift.hpp>
|
||||
#include <sprout/random/xor_combine.hpp>
|
||||
#include <sprout/random/taus88.hpp>
|
||||
#include <sprout/random/inversive_congruential.hpp>
|
||||
#include <sprout/random/mersenne_twister.hpp>
|
||||
#include <sprout/random/uniform_smallint.hpp>
|
||||
|
|
|
@ -144,7 +144,7 @@ namespace sprout {
|
|||
0xB,
|
||||
std::uint64_t(1) << 48
|
||||
> lcf_type;
|
||||
public:
|
||||
private:
|
||||
static SPROUT_CONSTEXPR result_type static_min() {
|
||||
return 0;
|
||||
}
|
||||
|
|
112
sprout/random/linear_feedback_shift.hpp
Normal file
112
sprout/random/linear_feedback_shift.hpp
Normal file
|
@ -0,0 +1,112 @@
|
|||
#ifndef SPROUT_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
|
||||
#define SPROUT_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
|
||||
|
||||
#include <ios>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/detail/integer/integer_mask.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
//
|
||||
// linear_feedback_shift_engine
|
||||
//
|
||||
template<typename UIntType, int w, int k, int q, int s>
|
||||
class linear_feedback_shift_engine {
|
||||
public:
|
||||
typedef UIntType result_type;
|
||||
private:
|
||||
struct private_constructor_tag {};
|
||||
public:
|
||||
SPROUT_STATIC_CONSTEXPR int word_size = w;
|
||||
SPROUT_STATIC_CONSTEXPR int exponent1 = k;
|
||||
SPROUT_STATIC_CONSTEXPR int exponent2 = q;
|
||||
SPROUT_STATIC_CONSTEXPR int step_size = s;
|
||||
SPROUT_STATIC_CONSTEXPR UIntType default_seed = 341;
|
||||
public:
|
||||
static_assert(w > 0, "w > 0");
|
||||
static_assert(q > 0, "q > 0");
|
||||
static_assert(k < w, "k < w");
|
||||
static_assert(0 < 2 * q && 2 * q < k, "0 < 2 * q && 2 * q < k");
|
||||
static_assert(0 < s && s <= k - q, "0 < s && s <= k - q");
|
||||
private:
|
||||
static SPROUT_CONSTEXPR UIntType wordmask() {
|
||||
return sprout::detail::low_bits_mask_t<w>::sig_bits;
|
||||
}
|
||||
static SPROUT_CONSTEXPR result_type static_min() {
|
||||
return 0;
|
||||
}
|
||||
static SPROUT_CONSTEXPR result_type static_max() {
|
||||
return wordmask();
|
||||
}
|
||||
static SPROUT_CONSTEXPR UIntType init_seed_1(UIntType const& x0) {
|
||||
return x0 < (1 << (w - k)) ? x0 + (1 << (w - k)) : x0;
|
||||
}
|
||||
static SPROUT_CONSTEXPR UIntType init_seed(UIntType const& x0) {
|
||||
return init_seed_1(x0 & wordmask());
|
||||
}
|
||||
private:
|
||||
UIntType x_;
|
||||
private:
|
||||
SPROUT_CONSTEXPR linear_feedback_shift_engine(UIntType const& x, private_constructor_tag)
|
||||
: x_(x)
|
||||
{}
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<linear_feedback_shift_engine> generate(result_type result) const {
|
||||
return sprout::random::random_result<linear_feedback_shift_engine>(
|
||||
result,
|
||||
linear_feedback_shift_engine(result, private_constructor_tag())
|
||||
);
|
||||
}
|
||||
public:
|
||||
SPROUT_CONSTEXPR linear_feedback_shift_engine()
|
||||
: x_(init_seed(default_seed))
|
||||
{}
|
||||
SPROUT_CONSTEXPR explicit linear_feedback_shift_engine(UIntType const& x0)
|
||||
: x_(init_seed(x0))
|
||||
{}
|
||||
SPROUT_CONSTEXPR result_type min() const {
|
||||
return static_min();
|
||||
}
|
||||
SPROUT_CONSTEXPR result_type max() const {
|
||||
return static_max();
|
||||
}
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<linear_feedback_shift_engine> operator()() const {
|
||||
return generate(((x_ & ((wordmask() << (w - k)) & wordmask())) << s) ^ ((((x_ << q) ^ x_) & wordmask()) >> (k - s)));
|
||||
}
|
||||
friend SPROUT_CONSTEXPR bool operator==(linear_feedback_shift_engine const& lhs, linear_feedback_shift_engine const& rhs) {
|
||||
return lhs.x_ == rhs.x_;
|
||||
}
|
||||
friend SPROUT_CONSTEXPR bool operator!=(linear_feedback_shift_engine const& lhs, linear_feedback_shift_engine const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
template<typename Elem, typename Traits>
|
||||
friend std::basic_istream<Elem, Traits>& operator>>(
|
||||
std::basic_istream<Elem, Traits>& lhs,
|
||||
linear_feedback_shift_engine& rhs
|
||||
)
|
||||
{
|
||||
return lhs >> rhs.x_;
|
||||
}
|
||||
template<typename Elem, typename Traits>
|
||||
friend std::basic_ostream<Elem, Traits>& operator<<(
|
||||
std::basic_ostream<Elem, Traits>& lhs,
|
||||
linear_feedback_shift_engine const& rhs
|
||||
)
|
||||
{
|
||||
return lhs << rhs.x_;
|
||||
}
|
||||
};
|
||||
template<typename UIntType, int w, int k, int q, int s>
|
||||
SPROUT_CONSTEXPR int sprout::random::linear_feedback_shift_engine<UIntType, w, k, q, s>::word_size;
|
||||
template<typename UIntType, int w, int k, int q, int s>
|
||||
SPROUT_CONSTEXPR int sprout::random::linear_feedback_shift_engine<UIntType, w, k, q, s>::exponent1;
|
||||
template<typename UIntType, int w, int k, int q, int s>
|
||||
SPROUT_CONSTEXPR int sprout::random::linear_feedback_shift_engine<UIntType, w, k, q, s>::exponent2;
|
||||
template<typename UIntType, int w, int k, int q, int s>
|
||||
SPROUT_CONSTEXPR int sprout::random::linear_feedback_shift_engine<UIntType, w, k, q, s>::step_size;
|
||||
template<typename UIntType, int w, int k, int q, int s>
|
||||
SPROUT_CONSTEXPR UIntType sprout::random::linear_feedback_shift_engine<UIntType, w, k, q, s>::default_seed;
|
||||
} // namespace random
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_RANDOM_LINEAR_FEEDBACK_SHIFT_HPP
|
30
sprout/random/taus88.hpp
Normal file
30
sprout/random/taus88.hpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
#ifndef SPROUT_RANDOM_TAUS88_HPP
|
||||
#define SPROUT_RANDOM_TAUS88_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/random/linear_feedback_shift.hpp>
|
||||
#include <sprout/random/xor_combine.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
//
|
||||
// taus88
|
||||
//
|
||||
typedef sprout::random::xor_combine_engine<
|
||||
sprout::random::xor_combine_engine<
|
||||
sprout::random::linear_feedback_shift_engine<std::uint32_t, 32, 31, 13, 12>,
|
||||
0,
|
||||
sprout::random::linear_feedback_shift_engine<std::uint32_t, 32, 29, 2, 4>,
|
||||
0
|
||||
>,
|
||||
0,
|
||||
sprout::random::linear_feedback_shift_engine<std::uint32_t, 32, 28, 3, 17>,
|
||||
0
|
||||
> taus88;
|
||||
} // namespace random
|
||||
|
||||
using sprout::random::taus88;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_RANDOM_TAUS88_HPP
|
90
sprout/random/xor_combine.hpp
Normal file
90
sprout/random/xor_combine.hpp
Normal file
|
@ -0,0 +1,90 @@
|
|||
#ifndef SPROUT_RANDOM_XOR_COMBINE_HPP
|
||||
#define SPROUT_RANDOM_XOR_COMBINE_HPP
|
||||
|
||||
#include <ios>
|
||||
#include <istream>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
//
|
||||
// xor_combine_engine
|
||||
//
|
||||
template<typename URNG1, int s1, typename URNG2, int s2>
|
||||
class xor_combine_engine {
|
||||
public:
|
||||
typedef URNG1 base1_type;
|
||||
typedef URNG2 base2_type;
|
||||
typedef typename base1_type::result_type result_type;
|
||||
public:
|
||||
SPROUT_STATIC_CONSTEXPR int shift1 = s1;
|
||||
SPROUT_STATIC_CONSTEXPR int shift2 = s2;
|
||||
private:
|
||||
base1_type rng1_;
|
||||
base2_type rng2_;
|
||||
private:
|
||||
template<typename Random1, typename Random2>
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<xor_combine_engine> generate(Random1 const& rnd1, Random2 const& rnd2) const {
|
||||
return sprout::random::random_result<xor_combine_engine>(
|
||||
(rnd1.result() << s1) ^ (rnd2.result() << s2),
|
||||
xor_combine_engine(
|
||||
rnd1.engine(),
|
||||
rnd2.engine()
|
||||
)
|
||||
);
|
||||
}
|
||||
public:
|
||||
SPROUT_CONSTEXPR xor_combine_engine()
|
||||
: rng1_()
|
||||
, rng2_()
|
||||
{}
|
||||
SPROUT_CONSTEXPR explicit xor_combine_engine(result_type const& seed)
|
||||
: rng1_(seed)
|
||||
, rng2_(seed)
|
||||
{}
|
||||
SPROUT_CONSTEXPR xor_combine_engine(base1_type const& rng1, base2_type const& rng2)
|
||||
: rng1_(rng1)
|
||||
, rng2_(rng2)
|
||||
{}
|
||||
SPROUT_CONSTEXPR result_type min() const {
|
||||
return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(rng1_.min(), rng2_.min());
|
||||
}
|
||||
SPROUT_CONSTEXPR result_type max() const {
|
||||
return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::max(rng1_.max(), rng2_.max());
|
||||
}
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<xor_combine_engine> operator()() const {
|
||||
return generate(rng1_(), rng2_());
|
||||
}
|
||||
friend SPROUT_CONSTEXPR bool operator==(xor_combine_engine const& lhs, xor_combine_engine const& rhs) {
|
||||
return lhs.rng1_ == rhs.rng1_ && lhs.rng2_ == rhs.rng2_;
|
||||
}
|
||||
friend SPROUT_CONSTEXPR bool operator!=(xor_combine_engine const& lhs, xor_combine_engine const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
template<typename Elem, typename Traits>
|
||||
friend std::basic_istream<Elem, Traits>& operator>>(
|
||||
std::basic_istream<Elem, Traits>& lhs,
|
||||
xor_combine_engine& rhs
|
||||
)
|
||||
{
|
||||
return lhs >> rhs.rng1_ >> std::ws >> rhs.rng2_;
|
||||
}
|
||||
template<typename Elem, typename Traits>
|
||||
friend std::basic_ostream<Elem, Traits>& operator<<(
|
||||
std::basic_ostream<Elem, Traits>& lhs,
|
||||
xor_combine_engine const& rhs
|
||||
)
|
||||
{
|
||||
return lhs << rhs.rng1_ << ' ' << rhs.rng2_;
|
||||
}
|
||||
};
|
||||
template<typename URNG1, int s1, typename URNG2, int s2>
|
||||
SPROUT_CONSTEXPR int sprout::random::xor_combine_engine<URNG1, s1, URNG2, s2>::shift1;
|
||||
template<typename URNG1, int s1, typename URNG2, int s2>
|
||||
SPROUT_CONSTEXPR int sprout::random::xor_combine_engine<URNG1, s1, URNG2, s2>::shift2;
|
||||
} // namespace random
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_RANDOM_XOR_COMBINE_HPP
|
Loading…
Reference in a new issue