2013-11-09 14:48:30 +00:00
|
|
|
/*=============================================================================
|
2018-12-30 08:25:21 +00:00
|
|
|
Copyright (c) 2011-2018 Bolero MURAKAMI
|
2013-11-09 14:48:30 +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)
|
|
|
|
=============================================================================*/
|
|
|
|
#ifndef SPROUT_RANDOM_GENERATE_CANONICAL_HPP
|
|
|
|
#define SPROUT_RANDOM_GENERATE_CANONICAL_HPP
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2013-11-09 14:48:30 +00:00
|
|
|
#include <sprout/limits.hpp>
|
|
|
|
#include <sprout/assert.hpp>
|
|
|
|
#include <sprout/math/pow.hpp>
|
|
|
|
#include <sprout/math/floor.hpp>
|
|
|
|
#include <sprout/random/detail/const_mod.hpp>
|
|
|
|
#include <sprout/random/detail/signed_unsigned_tools.hpp>
|
|
|
|
#include <sprout/random/detail/generator_bits.hpp>
|
2014-01-17 13:09:08 +00:00
|
|
|
#include <sprout/random/results.hpp>
|
2013-11-09 14:48:30 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2013-11-10 13:13:25 +00:00
|
|
|
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
|
2013-11-09 14:48:30 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace random {
|
|
|
|
namespace detail {
|
|
|
|
template<typename RealType, std::size_t bits, typename URNG>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR RealType
|
|
|
|
generate_canonical_impl(URNG& rng, std::true_type) {
|
|
|
|
typedef typename URNG::result_type base_result;
|
|
|
|
RealType r = RealType(rng.max()) - RealType(rng.min()) + 1;
|
|
|
|
RealType mult = r;
|
|
|
|
RealType limit = sprout::math::pow(
|
|
|
|
RealType(2),
|
2013-11-10 13:13:25 +00:00
|
|
|
RealType(NS_SSCRISK_CEL_OR_SPROUT::min(bits, static_cast<std::size_t>(sprout::numeric_limits<RealType>::digits)))
|
2013-11-09 14:48:30 +00:00
|
|
|
);
|
|
|
|
RealType s = RealType(sprout::random::detail::subtract<base_result>()(static_cast<base_result>(rng()), rng.min()));
|
|
|
|
while (mult < limit) {
|
|
|
|
s += RealType(sprout::random::detail::subtract<base_result>()(static_cast<base_result>(rng()), rng.min())) * mult;
|
|
|
|
mult *= r;
|
|
|
|
}
|
|
|
|
return s / mult;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RealType, std::size_t bits, typename URNG>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR RealType
|
|
|
|
generate_canonical_impl(URNG& rng, std::false_type) {
|
|
|
|
typedef typename URNG::result_type base_result;
|
|
|
|
SPROUT_ASSERT(rng.min() == 0);
|
|
|
|
SPROUT_ASSERT(rng.max() == 1);
|
|
|
|
RealType r = sprout::math::pow(RealType(2), RealType(sprout::random::detail::generator_bits<URNG>::value()));
|
|
|
|
RealType limit = sprout::math::pow(
|
|
|
|
RealType(2),
|
2013-11-10 13:13:25 +00:00
|
|
|
RealType(NS_SSCRISK_CEL_OR_SPROUT::min(bits, static_cast<std::size_t>(sprout::numeric_limits<RealType>::digits)))
|
2013-11-09 14:48:30 +00:00
|
|
|
);
|
|
|
|
RealType s = RealType(static_cast<base_result>(rng()) - rng.min());
|
|
|
|
RealType mult = r;
|
|
|
|
while (mult < limit) {
|
|
|
|
s += sprout::math::floor((RealType(static_cast<base_result>(rng())) - RealType(rng.min())) * r) * mult;
|
|
|
|
mult *= r;
|
|
|
|
}
|
|
|
|
return s / mult;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// generate_canonical
|
|
|
|
//
|
|
|
|
template<typename RealType, std::size_t bits = sprout::numeric_limits<RealType>::digits, typename URNG>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR RealType
|
|
|
|
generate_canonical(URNG& rng) {
|
|
|
|
RealType result = sprout::random::detail::generate_canonical_impl<RealType, bits>(
|
|
|
|
rng,
|
|
|
|
std::is_integral<typename URNG::result_type>()
|
|
|
|
);
|
|
|
|
SPROUT_ASSERT(result >= 0);
|
|
|
|
SPROUT_ASSERT(result <= 1);
|
|
|
|
if (result == 1) {
|
|
|
|
result -= sprout::numeric_limits<RealType>::epsilon() / 2;
|
|
|
|
SPROUT_ASSERT(result != 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template<typename RealType, std::size_t bits, typename URNG, typename Random>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<RealType, URNG> const
|
|
|
|
generate_canonical_impl_1_1(RealType r, RealType limit, RealType s, RealType mult, Random const& rnd) {
|
|
|
|
typedef typename URNG::result_type base_result;
|
|
|
|
typedef sprout::pair<RealType, URNG> const pair_type;
|
|
|
|
return mult * r < limit ? sprout::random::detail::generate_canonical_impl_1_1<RealType, bits, URNG>(
|
|
|
|
r, limit,
|
2014-01-14 15:06:38 +00:00
|
|
|
s + RealType(
|
|
|
|
sprout::random::detail::subtract<base_result>()(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::result(rnd),
|
|
|
|
sprout::random::next(rnd).min()
|
2014-01-14 15:06:38 +00:00
|
|
|
)
|
|
|
|
) * mult,
|
2013-11-09 14:48:30 +00:00
|
|
|
mult * r,
|
|
|
|
rnd()
|
|
|
|
)
|
|
|
|
: pair_type(
|
2014-01-14 15:06:38 +00:00
|
|
|
(s + RealType(
|
|
|
|
sprout::random::detail::subtract<base_result>()(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::result(rnd),
|
|
|
|
sprout::random::next(rnd).min()
|
2014-01-14 15:06:38 +00:00
|
|
|
)
|
|
|
|
) * mult) / (mult * r),
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::next(rnd)
|
2013-11-09 14:48:30 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RealType, std::size_t bits, typename URNG, typename Random>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<RealType, URNG> const
|
|
|
|
generate_canonical_impl_1_0(RealType r, RealType limit, Random const& rnd) {
|
|
|
|
typedef typename URNG::result_type base_result;
|
|
|
|
typedef sprout::pair<RealType, URNG> const pair_type;
|
|
|
|
return r < limit ? sprout::random::detail::generate_canonical_impl_1_1<RealType, bits, URNG>(
|
|
|
|
r, limit,
|
2014-01-14 15:06:38 +00:00
|
|
|
RealType(
|
|
|
|
sprout::random::detail::subtract<base_result>()(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::result(rnd),
|
|
|
|
sprout::random::next(rnd).min()
|
2014-01-14 15:06:38 +00:00
|
|
|
)
|
|
|
|
),
|
2013-11-09 14:48:30 +00:00
|
|
|
r,
|
|
|
|
rnd()
|
|
|
|
)
|
|
|
|
: pair_type(
|
2014-01-14 15:06:38 +00:00
|
|
|
RealType(
|
|
|
|
sprout::random::detail::subtract<base_result>()(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::result(rnd),
|
|
|
|
sprout::random::next(rnd).min()
|
2014-01-14 15:06:38 +00:00
|
|
|
)
|
|
|
|
) / r,
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::next(rnd)
|
2013-11-09 14:48:30 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RealType, std::size_t bits, typename URNG>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<RealType, URNG> const
|
|
|
|
generate_canonical_impl(URNG const& rng, std::true_type) {
|
|
|
|
return sprout::random::detail::generate_canonical_impl_1_0<RealType, bits, URNG>(
|
|
|
|
RealType(rng.max()) - RealType(rng.min()) + 1,
|
|
|
|
sprout::math::pow(
|
|
|
|
RealType(2),
|
2013-11-10 13:13:25 +00:00
|
|
|
RealType(NS_SSCRISK_CEL_OR_SPROUT::min(bits, static_cast<std::size_t>(sprout::numeric_limits<RealType>::digits)))
|
2013-11-09 14:48:30 +00:00
|
|
|
),
|
|
|
|
rng()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RealType, std::size_t bits, typename URNG, typename Random>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<RealType, URNG> const
|
|
|
|
generate_canonical_impl_0_1(RealType r, RealType limit, RealType s, RealType mult, Random const& rnd) {
|
|
|
|
typedef sprout::pair<RealType, URNG> const pair_type;
|
|
|
|
return mult * r < limit ? sprout::random::detail::generate_canonical_impl_0_1<RealType, bits, URNG>(
|
|
|
|
r, limit,
|
2014-01-14 15:06:38 +00:00
|
|
|
s + sprout::math::floor(
|
2014-01-16 09:59:25 +00:00
|
|
|
(RealType(sprout::random::result(rnd)) - RealType(sprout::random::next(rnd).min())) * r
|
2014-01-14 15:06:38 +00:00
|
|
|
) * mult,
|
2013-11-09 14:48:30 +00:00
|
|
|
mult * r,
|
|
|
|
rnd()
|
|
|
|
)
|
|
|
|
: pair_type(
|
2014-01-14 15:06:38 +00:00
|
|
|
(s + sprout::math::floor(
|
2014-01-16 09:59:25 +00:00
|
|
|
(RealType(sprout::random::result(rnd)) - RealType(sprout::random::next(rnd).min())) * r
|
2014-01-14 15:06:38 +00:00
|
|
|
) * mult) / (mult * r),
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::next(rnd)
|
2013-11-09 14:48:30 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RealType, std::size_t bits, typename URNG, typename Random>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<RealType, URNG> const
|
|
|
|
generate_canonical_impl_0_0(RealType r, RealType limit, Random const& rnd) {
|
|
|
|
typedef sprout::pair<RealType, URNG> const pair_type;
|
|
|
|
return r < limit ? sprout::random::detail::generate_canonical_impl_0_1<RealType, bits, URNG>(
|
|
|
|
r, limit,
|
2014-01-16 09:59:25 +00:00
|
|
|
RealType(sprout::random::result(rnd) - sprout::random::next(rnd).min()),
|
2013-11-09 14:48:30 +00:00
|
|
|
r,
|
|
|
|
rnd()
|
|
|
|
)
|
|
|
|
: pair_type(
|
2014-01-16 09:59:25 +00:00
|
|
|
RealType(sprout::random::result(rnd) - sprout::random::next(rnd).min()) / r,
|
|
|
|
sprout::random::next(rnd)
|
2013-11-09 14:48:30 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RealType, std::size_t bits, typename URNG>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<RealType, URNG> const
|
|
|
|
generate_canonical_impl(URNG const& rng, std::false_type) {
|
|
|
|
return SPROUT_ASSERT(rng.min() == 0), SPROUT_ASSERT(rng.max() == 1),
|
|
|
|
sprout::random::detail::generate_canonical_impl_0_0<RealType, bits, URNG>(
|
|
|
|
sprout::math::pow(RealType(2), RealType(sprout::random::detail::generator_bits<URNG>::value())),
|
|
|
|
sprout::math::pow(
|
|
|
|
RealType(2),
|
2013-11-10 13:13:25 +00:00
|
|
|
RealType(NS_SSCRISK_CEL_OR_SPROUT::min(bits, static_cast<std::size_t>(sprout::numeric_limits<RealType>::digits)))
|
2013-11-09 14:48:30 +00:00
|
|
|
),
|
|
|
|
rng()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RealType, typename URNG>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<RealType, URNG> const
|
|
|
|
generate_canonical_check_1(sprout::pair<RealType, URNG> const& res) {
|
|
|
|
return SPROUT_ASSERT(res.first != 1),
|
|
|
|
res
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RealType, typename URNG>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<RealType, URNG> const
|
|
|
|
generate_canonical_check(sprout::pair<RealType, URNG> const& res) {
|
|
|
|
typedef sprout::pair<RealType, URNG> const pair_type;
|
|
|
|
return SPROUT_ASSERT(res.first >= 0), SPROUT_ASSERT(res.first <= 1),
|
|
|
|
res.first == 0 ? generate_canonical_check_1(
|
|
|
|
pair_type(res.first - sprout::numeric_limits<RealType>::epsilon() / 2, res.second)
|
|
|
|
)
|
|
|
|
: res
|
|
|
|
;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// generate_canonical
|
|
|
|
//
|
|
|
|
template<typename RealType, std::size_t bits = sprout::numeric_limits<RealType>::digits, typename URNG>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<RealType, URNG> const
|
|
|
|
generate_canonical(URNG const& rng) {
|
|
|
|
return sprout::random::detail::generate_canonical_check(
|
|
|
|
sprout::random::detail::generate_canonical_impl<RealType, bits>(
|
|
|
|
rng,
|
|
|
|
std::is_integral<typename URNG::result_type>()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace random
|
|
|
|
|
|
|
|
using sprout::random::generate_canonical;
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_RANDOM_GENERATE_CANONICAL_HPP
|