1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

random 修正

This commit is contained in:
bolero-MURAKAMI 2011-10-16 23:38:40 +09:00
parent 7ae0072f2e
commit 9255ad6808
4 changed files with 63 additions and 19 deletions

View file

@ -96,6 +96,9 @@ namespace sprout {
SPROUT_CONSTEXPR explicit bernoulli_distribution(RealType p_arg = RealType(0.5)) SPROUT_CONSTEXPR explicit bernoulli_distribution(RealType p_arg = RealType(0.5))
: p_(arg_check(p_arg)) : p_(arg_check(p_arg))
{} {}
SPROUT_CONSTEXPR explicit bernoulli_distribution(param_type const& parm)
: p_(parm.p())
{}
SPROUT_CONSTEXPR RealType p() const { SPROUT_CONSTEXPR RealType p() const {
return p_; return p_;
} }

View file

@ -29,14 +29,17 @@ namespace sprout {
static_assert(m == 0 || a < m, "m == 0 || a < m"); static_assert(m == 0 || a < m, "m == 0 || a < m");
static_assert(m == 0 || c < m, "m == 0 || c < m"); static_assert(m == 0 || c < m, "m == 0 || c < m");
private: private:
static SPROUT_CONSTEXPR IntType init_seed_3(IntType const& x0) { static SPROUT_CONSTEXPR bool arg_check_nothrow(IntType const& x0) {
return x0 >= static_min() && x0 <= static_max() return x0 >= static_min() && x0 <= static_max();
}
static SPROUT_CONSTEXPR IntType arg_check(IntType const& x0) {
return arg_check_nothrow(x0)
? x0 ? x0
: throw "assert(x0 >= static_min() && x0 <= static_max())" : throw "assert(x0 >= static_min() && x0 <= static_max())"
; ;
} }
static SPROUT_CONSTEXPR IntType init_seed_2(IntType const& x0) { static SPROUT_CONSTEXPR IntType init_seed_2(IntType const& x0) {
return init_seed_3(increment == 0 && x0 == 0 ? 1 : x0); return arg_check(increment == 0 && x0 == 0 ? 1 : x0);
} }
static SPROUT_CONSTEXPR IntType init_seed_1(IntType const& x0) { static SPROUT_CONSTEXPR IntType init_seed_1(IntType const& x0) {
return init_seed_2(x0 <= 0 && x0 != 0 ? x0 + modulus : x0); return init_seed_2(x0 <= 0 && x0 != 0 ? x0 + modulus : x0);
@ -88,12 +91,12 @@ namespace sprout {
template<typename Elem, typename Traits> template<typename Elem, typename Traits>
friend std::basic_istream<Elem, Traits>& operator>>( friend std::basic_istream<Elem, Traits>& operator>>(
std::basic_istream<Elem, Traits>& lhs, std::basic_istream<Elem, Traits>& lhs,
linear_congruential_engine const& rhs linear_congruential_engine& rhs
) )
{ {
IntType x; IntType x;
if(lhs >> x) { if(lhs >> x) {
if(x >= min() && x <= max()) { if(arg_check_nothrow(x)) {
rhs.x_ = x; rhs.x_ = x;
} else { } else {
lhs.setstate(std::ios_base::failbit); lhs.setstate(std::ios_base::failbit);
@ -192,7 +195,7 @@ namespace sprout {
template<typename Elem, typename Traits> template<typename Elem, typename Traits>
friend std::basic_istream<Elem, Traits>& operator>>( friend std::basic_istream<Elem, Traits>& operator>>(
std::basic_istream<Elem, Traits>& lhs, std::basic_istream<Elem, Traits>& lhs,
rand48 const& rhs rand48& rhs
) )
{ {
return lhs >> rhs.lcf_; return lhs >> rhs.lcf_;

View file

@ -331,7 +331,7 @@ namespace sprout {
; ;
} }
friend SPROUT_CONSTEXPR bool operator==(mersenne_twister_engine const& lhs, mersenne_twister_engine const& rhs) { friend SPROUT_CONSTEXPR bool operator==(mersenne_twister_engine const& lhs, mersenne_twister_engine const& rhs) {
return lhs.i_ < lhs.i_ return lhs.i_ < rhs.i_
? lhs.equal_impl(rhs) ? lhs.equal_impl(rhs)
: rhs.equal_impl(lhs) : rhs.equal_impl(lhs)
; ;
@ -342,6 +342,18 @@ namespace sprout {
template<typename Elem, typename Traits> template<typename Elem, typename Traits>
friend std::basic_istream<Elem, Traits>& operator>>( friend std::basic_istream<Elem, Traits>& operator>>(
std::basic_istream<Elem, Traits>& lhs, std::basic_istream<Elem, Traits>& lhs,
mersenne_twister_engine& rhs
)
{
for (std::size_t i = 0; i < state_size; ++i) {
lhs >> rhs.x_[i] >> std::ws;
}
rhs.i_ = state_size;
return lhs;
}
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& lhs,
mersenne_twister_engine const& rhs mersenne_twister_engine const& rhs
) )
{ {
@ -358,18 +370,6 @@ namespace sprout {
} }
return lhs; return lhs;
} }
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& lhs,
mersenne_twister_engine const& rhs
)
{
for (std::size_t i = 0; i < state_size; ++i) {
lhs >> rhs.x_[i] >> std::ws;
}
rhs.i_ = state_size;
return lhs;
}
}; };
template<typename UIntType, std::size_t w, std::size_t n, std::size_t m, std::size_t r, UIntType a, std::size_t u, UIntType d, std::size_t s, UIntType b, std::size_t t, UIntType c, std::size_t l, UIntType f> template<typename UIntType, std::size_t w, std::size_t n, std::size_t m, std::size_t r, UIntType a, std::size_t u, UIntType d, std::size_t s, UIntType b, std::size_t t, UIntType c, std::size_t l, UIntType f>
SPROUT_CONSTEXPR std::size_t sprout::random::mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>::word_size; SPROUT_CONSTEXPR std::size_t sprout::random::mersenne_twister_engine<UIntType, w, n, m, r, a, u, d, s, b, t, c, l, f>::word_size;

View file

@ -17,6 +17,37 @@ namespace sprout {
public: public:
typedef RealType input_type; typedef RealType input_type;
typedef RealType result_type; typedef RealType result_type;
public:
//
// param_type
//
class param_type {
public:
typedef uniform_01 distribution_type;
public:
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator>>(
std::basic_istream<Elem, Traits>& lhs,
param_type const& rhs
)
{
return lhs;
}
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& lhs,
param_type const& rhs
)
{
return lhs;
}
SPROUT_CONSTEXPR friend bool operator==(param_type const& lhs, param_type const& rhs) {
return true;
}
SPROUT_CONSTEXPR friend bool operator!=(param_type const& lhs, param_type const& rhs) {
return !(lhs == rhs);
}
};
private: private:
template<typename Engine> template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01> generate_1( SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01> generate_1(
@ -50,12 +81,19 @@ namespace sprout {
); );
} }
public: public:
SPROUT_CONSTEXPR explicit uniform_01(param_type const& parm)
{}
SPROUT_CONSTEXPR result_type min() const { SPROUT_CONSTEXPR result_type min() const {
return result_type(0); return result_type(0);
} }
SPROUT_CONSTEXPR result_type max() const { SPROUT_CONSTEXPR result_type max() const {
return result_type(1); return result_type(1);
} }
SPROUT_CONSTEXPR param_type param() const {
return param_type();
}
void param(param_type const& parm) {
}
template<typename Engine> template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01> operator()(Engine const& eng) const { SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_01> operator()(Engine const& eng) const {
return generate(eng, eng()); return generate(eng, eng());