Sprout/sprout/random/detail/uniform_int_float.hpp

85 lines
2.9 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2017 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-12 20:28:33 +00:00
#ifndef SPROUT_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
#define SPROUT_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
#include <cstdint>
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
2013-08-06 15:15:09 +00:00
#include <sprout/limits.hpp>
2011-10-12 20:28:33 +00:00
#include <sprout/random/random_result.hpp>
#include <sprout/random/detail/generator_bits.hpp>
#include <sprout/random/results.hpp>
2011-10-12 20:28:33 +00:00
#include <sprout/detail/integer.hpp>
2013-02-19 16:12:56 +00:00
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
2011-10-12 20:28:33 +00:00
namespace sprout {
namespace random {
namespace detail {
2014-01-15 16:04:31 +00:00
template<typename URNG>
2011-10-12 20:28:33 +00:00
class uniform_int_float {
public:
typedef URNG base_type;
typedef typename base_type::result_type base_result;
typedef typename sprout::detail::uint_t<
2013-08-06 15:15:09 +00:00
(sprout::numeric_limits<std::uintmax_t>::digits < sprout::numeric_limits<base_result>::digits)
? sprout::numeric_limits<std::uintmax_t>::digits
: sprout::numeric_limits<base_result>::digits
2011-10-12 20:28:33 +00:00
>::fast result_type;
private:
base_type rng_;
private:
SPROUT_CONSTEXPR sprout::random::random_result<uniform_int_float> generate(
sprout::random::random_result<base_type> const& rnd
) const
{
return sprout::random::random_result<uniform_int_float>(
2014-01-16 09:59:25 +00:00
static_cast<result_type>(sprout::random::result(rnd) * (static_cast<base_result>(max()) + 1)),
uniform_int_float(sprout::random::next(rnd))
);
}
2011-10-12 20:28:33 +00:00
public:
SPROUT_CONSTEXPR uniform_int_float()
: rng_()
{}
2012-04-11 14:28:29 +00:00
explicit SPROUT_CONSTEXPR uniform_int_float(base_type const& rng)
2011-10-12 20:28:33 +00:00
: rng_(rng)
{}
SPROUT_CONSTEXPR result_type min() const {
return 0;
}
SPROUT_CONSTEXPR result_type max() const {
return (
result_type(2) << (
2012-04-01 13:15:09 +00:00
NS_SSCRISK_CEL_OR_SPROUT::min(
2013-08-06 15:15:09 +00:00
sprout::numeric_limits<result_type>::digits,
2011-10-12 20:28:33 +00:00
sprout::random::detail::generator_bits<base_type>::value()
) - 1
)
) - 1
;
}
base_type& base() {
return rng_;
}
SPROUT_CONSTEXPR base_type const& base() const {
return rng_;
}
SPROUT_CXX14_CONSTEXPR result_type operator()() {
return static_cast<result_type>(static_cast<base_result>(rng_()) * static_cast<base_result>(max()) + 1);
2011-10-12 20:28:33 +00:00
}
SPROUT_CONSTEXPR sprout::random::random_result<uniform_int_float> const operator()() const {
2011-10-12 20:28:33 +00:00
return generate(rng_());
}
};
2013-03-22 05:24:19 +00:00
} // namespace detail
} // namespace random
} // namespace sprout
2011-10-12 20:28:33 +00:00
2013-03-22 05:24:19 +00:00
#endif // #ifndef SPROUT_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP