2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-04-09 09:11:40 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
2013-08-08 09:54:33 +00:00
|
|
|
|
2014-04-09 09:11:40 +00:00
|
|
|
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)
|
2013-08-08 09:54:33 +00:00
|
|
|
=============================================================================*/
|
2011-10-12 20:28:33 +00:00
|
|
|
#ifndef SPROUT_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
|
|
|
|
#define SPROUT_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
|
|
|
|
|
|
|
|
#include <istream>
|
2014-11-28 07:19:07 +00:00
|
|
|
#include <ostream>
|
2011-10-12 20:28:33 +00:00
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/random/detail/signed_unsigned_tools.hpp>
|
|
|
|
#include <sprout/random/random_result.hpp>
|
2014-01-17 13:09:08 +00:00
|
|
|
#include <sprout/random/results.hpp>
|
2013-03-18 10:12:21 +00:00
|
|
|
#include <sprout/assert.hpp>
|
2011-10-12 20:28:33 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace random {
|
|
|
|
namespace detail {
|
|
|
|
template<typename T, typename Engine>
|
|
|
|
struct generate_uniform_real_result {
|
|
|
|
public:
|
|
|
|
T result;
|
|
|
|
Engine engine;
|
|
|
|
};
|
|
|
|
|
2013-11-05 14:46:23 +00:00
|
|
|
template<typename Engine, typename T>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR T
|
|
|
|
generate_uniform_real(
|
|
|
|
Engine& eng, T min_value, T max_value,
|
|
|
|
std::false_type
|
|
|
|
)
|
|
|
|
{
|
2014-08-01 06:24:00 +00:00
|
|
|
for (; ; ) {
|
2013-11-05 14:46:23 +00:00
|
|
|
typedef T result_type;
|
|
|
|
typedef typename Engine::result_type base_result;
|
|
|
|
result_type numerator = static_cast<T>(static_cast<base_result>(eng()) - eng.min());
|
|
|
|
result_type divisor = static_cast<T>(eng.max() - eng.min());
|
|
|
|
SPROUT_ASSERT(divisor > 0);
|
|
|
|
SPROUT_ASSERT(numerator >= 0 && numerator <= divisor);
|
|
|
|
result_type result = numerator / divisor * (max_value - min_value) + min_value;
|
|
|
|
if (result < max_value) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR T
|
|
|
|
generate_uniform_real(
|
|
|
|
Engine& eng, T min_value, T max_value,
|
|
|
|
std::true_type
|
|
|
|
)
|
|
|
|
{
|
2014-08-01 06:24:00 +00:00
|
|
|
for (; ; ) {
|
2013-11-05 14:46:23 +00:00
|
|
|
typedef T result_type;
|
|
|
|
typedef typename Engine::result_type base_result;
|
|
|
|
result_type numerator = static_cast<T>(sprout::random::detail::subtract<base_result>()(static_cast<base_result>(eng()), eng.min()));
|
|
|
|
result_type divisor = static_cast<T>(sprout::random::detail::subtract<base_result>()(eng.max(), eng.min())) + 1;
|
|
|
|
SPROUT_ASSERT(divisor > 0);
|
|
|
|
SPROUT_ASSERT(numerator >= 0 && numerator <= divisor);
|
|
|
|
result_type result = numerator / divisor * (max_value - min_value) + min_value;
|
|
|
|
if (result < max_value) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR T
|
|
|
|
generate_uniform_real(
|
|
|
|
Engine& eng, T min_value, T max_value
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_real(
|
|
|
|
eng, min_value, max_value,
|
|
|
|
typename std::is_integral<typename Engine::result_type>::type()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-07-25 14:32:42 +00:00
|
|
|
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D = 16, typename EngineResult, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
|
|
|
);
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D = 16, typename EngineResult, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
|
|
|
);
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_3(
|
|
|
|
Engine const& eng,
|
|
|
|
T min_value, T max_value,
|
|
|
|
T result
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return result < max_value
|
|
|
|
? sprout::random::detail::generate_uniform_real_result<T, Engine>{result, eng}
|
|
|
|
: sprout::random::detail::generate_uniform_real_false_1<D + 1>(eng(), min_value, max_value)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_3(
|
|
|
|
Engine const&,
|
|
|
|
T, T,
|
|
|
|
T
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_2(
|
|
|
|
Engine const& eng,
|
|
|
|
T min_value, T max_value,
|
|
|
|
T numerator, T divisor
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return SPROUT_ASSERT(divisor > 0), SPROUT_ASSERT(numerator >= 0 && numerator <= divisor),
|
|
|
|
sprout::random::detail::generate_uniform_real_false_3<D + 1>(
|
|
|
|
eng,
|
|
|
|
min_value, max_value,
|
|
|
|
numerator / divisor * (max_value - min_value) + min_value
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_2(
|
|
|
|
Engine const&,
|
|
|
|
T, T,
|
|
|
|
T, T
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE_DECL(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_real_false_2<D + 1>(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::next(rnd),
|
2013-07-25 14:32:42 +00:00
|
|
|
min_value, max_value,
|
2014-01-16 09:59:25 +00:00
|
|
|
static_cast<T>(sprout::random::result(rnd) - sprout::random::next(rnd).min()),
|
|
|
|
static_cast<T>(sprout::random::next(rnd).max() - sprout::random::next(rnd).min())
|
2013-07-25 14:32:42 +00:00
|
|
|
);
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK_DECL(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const&,
|
2013-07-25 14:32:42 +00:00
|
|
|
T, T
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D = 16, typename EngineResult, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
|
|
|
);
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D = 16, typename EngineResult, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
|
|
|
);
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_3(
|
|
|
|
Engine const& eng,
|
|
|
|
T min_value, T max_value,
|
|
|
|
T result
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return result < max_value
|
|
|
|
? sprout::random::detail::generate_uniform_real_result<T, Engine>{result, eng}
|
|
|
|
: sprout::random::detail::generate_uniform_real_true_1<D + 1>(eng(), min_value, max_value)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_3(
|
|
|
|
Engine const&,
|
|
|
|
T, T,
|
|
|
|
T
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_2(
|
|
|
|
Engine const& eng,
|
|
|
|
T min_value, T max_value,
|
|
|
|
T numerator, T divisor
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return SPROUT_ASSERT(divisor > 0), SPROUT_ASSERT(numerator >= 0 && numerator <= divisor),
|
|
|
|
sprout::random::detail::generate_uniform_real_true_3<D + 1>(
|
|
|
|
eng,
|
|
|
|
min_value, max_value,
|
|
|
|
numerator / divisor * (max_value - min_value) + min_value
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D, typename Engine, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_2(
|
|
|
|
Engine const&,
|
|
|
|
T, T,
|
|
|
|
T, T
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE_DECL(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
|
|
|
)
|
|
|
|
{
|
2013-11-04 09:50:48 +00:00
|
|
|
typedef typename EngineResult::result_type base_result;
|
2013-07-25 14:32:42 +00:00
|
|
|
return sprout::random::detail::generate_uniform_real_true_2<D + 1>(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::next(rnd),
|
2013-07-25 14:32:42 +00:00
|
|
|
min_value, max_value,
|
2014-01-16 09:59:25 +00:00
|
|
|
static_cast<T>(sprout::random::detail::subtract<base_result>()(sprout::random::result(rnd), sprout::random::next(rnd).min())),
|
|
|
|
static_cast<T>(sprout::random::detail::subtract<base_result>()(sprout::random::next(rnd).max(), sprout::random::next(rnd).min())) + 1
|
2013-07-25 14:32:42 +00:00
|
|
|
);
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<int D, typename EngineResult, typename T, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK_DECL(D)>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const&,
|
2013-07-25 14:32:42 +00:00
|
|
|
T, T
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
#else
|
2013-11-04 09:50:48 +00:00
|
|
|
template<typename EngineResult, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
2011-10-12 20:28:33 +00:00
|
|
|
);
|
|
|
|
template<typename Engine, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_3(
|
2011-10-12 20:28:33 +00:00
|
|
|
Engine const& eng,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value,
|
2011-10-12 20:28:33 +00:00
|
|
|
T result
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return result < max_value
|
|
|
|
? sprout::random::detail::generate_uniform_real_result<T, Engine>{result, eng}
|
|
|
|
: sprout::random::detail::generate_uniform_real_false_1(eng(), min_value, max_value)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_2(
|
2011-10-12 20:28:33 +00:00
|
|
|
Engine const& eng,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value,
|
|
|
|
T numerator, T divisor
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
2013-03-18 10:12:21 +00:00
|
|
|
return SPROUT_ASSERT(divisor > 0), SPROUT_ASSERT(numerator >= 0 && numerator <= divisor),
|
|
|
|
sprout::random::detail::generate_uniform_real_false_3(
|
|
|
|
eng,
|
2013-07-25 14:32:42 +00:00
|
|
|
min_value, max_value,
|
2013-03-18 10:12:21 +00:00
|
|
|
numerator / divisor * (max_value - min_value) + min_value
|
|
|
|
)
|
2011-10-12 20:28:33 +00:00
|
|
|
;
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<typename EngineResult, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_false_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_real_false_2(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::next(rnd),
|
2013-07-25 14:32:42 +00:00
|
|
|
min_value, max_value,
|
2014-01-16 09:59:25 +00:00
|
|
|
static_cast<T>(sprout::random::result(rnd) - sprout::random::next(rnd).min()),
|
|
|
|
static_cast<T>(sprout::random::next(rnd).max() - sprout::random::next(rnd).min())
|
2011-10-12 20:28:33 +00:00
|
|
|
);
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<typename EngineResult, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
2011-10-12 20:28:33 +00:00
|
|
|
);
|
|
|
|
template<typename Engine, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_3(
|
2011-10-12 20:28:33 +00:00
|
|
|
Engine const& eng,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value,
|
2011-10-12 20:28:33 +00:00
|
|
|
T result
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return result < max_value
|
|
|
|
? sprout::random::detail::generate_uniform_real_result<T, Engine>{result, eng}
|
|
|
|
: sprout::random::detail::generate_uniform_real_true_1(eng(), min_value, max_value)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_2(
|
2011-10-12 20:28:33 +00:00
|
|
|
Engine const& eng,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value,
|
|
|
|
T numerator, T divisor
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
2013-03-18 10:12:21 +00:00
|
|
|
return SPROUT_ASSERT(divisor > 0), SPROUT_ASSERT(numerator >= 0 && numerator <= divisor),
|
|
|
|
sprout::random::detail::generate_uniform_real_true_3(
|
|
|
|
eng,
|
2013-07-25 14:32:42 +00:00
|
|
|
min_value, max_value,
|
2013-03-18 10:12:21 +00:00
|
|
|
numerator / divisor * (max_value - min_value) + min_value
|
|
|
|
)
|
2011-10-12 20:28:33 +00:00
|
|
|
;
|
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
template<typename EngineResult, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, typename EngineResult::engine_type>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real_true_1(
|
2013-11-04 09:50:48 +00:00
|
|
|
EngineResult const& rnd,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
2013-11-04 09:50:48 +00:00
|
|
|
typedef typename EngineResult::result_type base_result;
|
2011-10-12 20:28:33 +00:00
|
|
|
return sprout::random::detail::generate_uniform_real_true_2(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::next(rnd),
|
2013-07-25 14:32:42 +00:00
|
|
|
min_value, max_value,
|
2014-01-16 09:59:25 +00:00
|
|
|
static_cast<T>(sprout::random::detail::subtract<base_result>()(sprout::random::result(rnd), sprout::random::next(rnd).min())),
|
|
|
|
static_cast<T>(sprout::random::detail::subtract<base_result>()(sprout::random::next(rnd).max(), sprout::random::next(rnd).min())) + 1
|
2011-10-12 20:28:33 +00:00
|
|
|
);
|
|
|
|
}
|
2013-07-25 14:32:42 +00:00
|
|
|
#endif
|
|
|
|
template<typename Engine, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real(
|
|
|
|
Engine const& eng,
|
|
|
|
T min_value, T max_value,
|
|
|
|
std::false_type
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_real_false_1(
|
|
|
|
eng(),
|
|
|
|
min_value, max_value
|
|
|
|
);
|
|
|
|
}
|
2011-10-12 20:28:33 +00:00
|
|
|
template<typename Engine, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real(
|
2011-10-12 20:28:33 +00:00
|
|
|
Engine const& eng,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value,
|
2011-10-12 20:28:33 +00:00
|
|
|
std::true_type
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_real_true_1(
|
|
|
|
eng(),
|
2013-07-25 14:32:42 +00:00
|
|
|
min_value, max_value
|
2011-10-12 20:28:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename Engine, typename T>
|
2013-11-05 14:46:23 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::random::detail::generate_uniform_real_result<T, Engine>
|
2013-07-25 14:32:42 +00:00
|
|
|
generate_uniform_real(
|
2011-10-12 20:28:33 +00:00
|
|
|
Engine const& eng,
|
2013-07-25 14:32:42 +00:00
|
|
|
T min_value, T max_value
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::random::detail::generate_uniform_real(
|
|
|
|
eng,
|
2013-07-25 14:32:42 +00:00
|
|
|
min_value, max_value,
|
2013-11-05 14:46:23 +00:00
|
|
|
typename std::is_integral<typename Engine::result_type>::type()
|
2011-10-12 20:28:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// uniform_real_distribution
|
|
|
|
//
|
|
|
|
template<typename RealType = double>
|
|
|
|
class uniform_real_distribution {
|
|
|
|
public:
|
|
|
|
typedef RealType input_type;
|
|
|
|
typedef RealType result_type;
|
|
|
|
public:
|
|
|
|
//
|
|
|
|
// param_type
|
|
|
|
//
|
|
|
|
class param_type {
|
|
|
|
public:
|
|
|
|
typedef uniform_real_distribution distribution_type;
|
|
|
|
private:
|
|
|
|
RealType min_;
|
|
|
|
RealType max_;
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR param_type()
|
|
|
|
: min_(RealType(0.0))
|
|
|
|
, max_(RealType(1.0))
|
|
|
|
{}
|
2014-08-01 06:24:00 +00:00
|
|
|
param_type(param_type const&) = default;
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR param_type(RealType min_arg, RealType max_arg = RealType(1.0))
|
2013-03-18 10:12:21 +00:00
|
|
|
: min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg))
|
2011-10-12 20:28:33 +00:00
|
|
|
, max_(max_arg)
|
|
|
|
{}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR RealType a() const SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return min_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR RealType b() const SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return max_;
|
|
|
|
}
|
|
|
|
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-12 20:28:33 +00:00
|
|
|
std::basic_istream<Elem, Traits>& lhs,
|
2011-10-17 04:44:19 +00:00
|
|
|
param_type& rhs
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
2011-10-14 12:23:13 +00:00
|
|
|
RealType min;
|
|
|
|
RealType max;
|
|
|
|
if (lhs >> min >> std::ws >> max) {
|
2013-03-18 10:12:21 +00:00
|
|
|
if (min <= max) {
|
2011-10-14 12:23:13 +00:00
|
|
|
rhs.min_ = min;
|
|
|
|
rhs.max_ = max;
|
|
|
|
} else {
|
|
|
|
lhs.setstate(std::ios_base::failbit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lhs;
|
2011-10-12 20:28:33 +00:00
|
|
|
}
|
|
|
|
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-12 20:28:33 +00:00
|
|
|
std::basic_ostream<Elem, Traits>& lhs,
|
|
|
|
param_type const& rhs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return lhs << rhs.min_ << " " << rhs.max_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator==(param_type const& lhs, param_type const& rhs) SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return lhs.min_ == rhs.min_ && lhs.max_ == rhs.max_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator!=(param_type const& lhs, param_type const& rhs) SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private:
|
2011-10-19 13:47:59 +00:00
|
|
|
RealType min_;
|
|
|
|
RealType max_;
|
2011-10-12 20:28:33 +00:00
|
|
|
private:
|
|
|
|
template<typename Engine, typename Result>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_real_distribution> generate(Result const& rnd) const {
|
|
|
|
return sprout::random::random_result<Engine, uniform_real_distribution>(
|
|
|
|
rnd.result,
|
|
|
|
rnd.engine,
|
|
|
|
*this
|
|
|
|
);
|
|
|
|
}
|
|
|
|
public:
|
2013-11-05 14:46:23 +00:00
|
|
|
SPROUT_CONSTEXPR uniform_real_distribution() SPROUT_NOEXCEPT
|
2011-10-12 20:28:33 +00:00
|
|
|
: min_(RealType(0.0))
|
|
|
|
, max_(RealType(1.0))
|
|
|
|
{}
|
2014-08-01 06:24:00 +00:00
|
|
|
uniform_real_distribution(uniform_real_distribution const&) = default;
|
2012-04-11 14:28:29 +00:00
|
|
|
explicit SPROUT_CONSTEXPR uniform_real_distribution(RealType min_arg, RealType max_arg = RealType(1.0))
|
2013-03-18 10:12:21 +00:00
|
|
|
: min_((SPROUT_ASSERT(min_arg <= max_arg), min_arg))
|
2011-10-12 20:28:33 +00:00
|
|
|
, max_(max_arg)
|
|
|
|
{}
|
2013-11-05 14:46:23 +00:00
|
|
|
explicit SPROUT_CONSTEXPR uniform_real_distribution(param_type const& parm) SPROUT_NOEXCEPT
|
2011-10-12 20:28:33 +00:00
|
|
|
: min_(parm.a())
|
|
|
|
, max_(parm.b())
|
|
|
|
{}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type a() const SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return min_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type b() const SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return max_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type min() const SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return min_;
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR result_type max() const SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return max_;
|
|
|
|
}
|
2013-11-09 09:13:26 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void reset() SPROUT_NOEXCEPT {}
|
2012-11-16 04:40:19 +00:00
|
|
|
SPROUT_CONSTEXPR param_type param() const SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return param_type(min_, max_);
|
|
|
|
}
|
2013-11-02 09:28:18 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void param(param_type const& parm) {
|
2011-10-12 20:28:33 +00:00
|
|
|
min_ = parm.a();
|
|
|
|
max_ = parm.b();
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
2013-11-05 14:46:23 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng) const {
|
|
|
|
return sprout::random::detail::generate_uniform_real(eng, min_, max_);
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
2013-11-04 09:50:48 +00:00
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_real_distribution> const operator()(Engine const& eng) const {
|
2011-10-12 20:28:33 +00:00
|
|
|
return generate<Engine>(sprout::random::detail::generate_uniform_real(eng, min_, max_));
|
|
|
|
}
|
2013-11-05 14:46:23 +00:00
|
|
|
template<typename Engine>
|
|
|
|
SPROUT_CXX14_CONSTEXPR result_type operator()(Engine& eng, param_type const& parm) const {
|
|
|
|
return sprout::random::detail::generate_uniform_real(eng, parm.a(), parm.b());
|
|
|
|
}
|
|
|
|
template<typename Engine>
|
|
|
|
SPROUT_CONSTEXPR sprout::random::random_result<Engine, uniform_real_distribution> const operator()(Engine const& eng, param_type const& parm) const {
|
|
|
|
return generate<Engine>(sprout::random::detail::generate_uniform_real(eng, parm.a(), parm.b()));
|
|
|
|
}
|
2011-10-12 20:28:33 +00:00
|
|
|
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-12 20:28:33 +00:00
|
|
|
std::basic_istream<Elem, Traits>& lhs,
|
2011-10-17 04:44:19 +00:00
|
|
|
uniform_real_distribution& rhs
|
2011-10-12 20:28:33 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
param_type parm;
|
2011-10-19 13:47:59 +00:00
|
|
|
if (lhs >> parm) {
|
|
|
|
rhs.param(parm);
|
|
|
|
}
|
2011-10-12 20:28:33 +00:00
|
|
|
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-12 20:28:33 +00:00
|
|
|
std::basic_ostream<Elem, Traits>& lhs,
|
|
|
|
uniform_real_distribution const& rhs
|
|
|
|
)
|
|
|
|
{
|
2011-10-17 04:44:19 +00:00
|
|
|
return lhs << rhs.param();
|
2011-10-12 20:28:33 +00:00
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator==(uniform_real_distribution const& lhs, uniform_real_distribution const& rhs) SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return lhs.param() == rhs.param();
|
|
|
|
}
|
2012-11-16 04:40:19 +00:00
|
|
|
friend SPROUT_CONSTEXPR bool operator!=(uniform_real_distribution const& lhs, uniform_real_distribution const& rhs) SPROUT_NOEXCEPT {
|
2011-10-12 20:28:33 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace random
|
|
|
|
|
|
|
|
using sprout::random::uniform_real_distribution;
|
|
|
|
} // namespace sprout
|
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
#endif // #ifndef SPROUT_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP
|