12 #if !defined(RANDOMLIB_EXPONENTIALPROB_HPP)
13 #define RANDOMLIB_EXPONENTIALPROB_HPP 1
20 # pragma warning (push)
21 # pragma warning (disable: 4127)
63 typedef unsigned word;
79 template<
typename RealType,
class Random>
86 template<
typename RealType,
class Random>
87 bool ExpFraction(
Random& r, RealType p)
const;
91 mutable std::vector<word> _v;
95 static const unsigned alloc_incr = 16;
98 template<
typename RealType,
class Random>
101 "ExponentialProb(): invalid real type RealType");
104 ( p < RealType(1)/std::numeric_limits<RealType>::epsilon() &&
106 ExpFraction(r, p < RealType(1) ? p : RealType(1)) &&
107 ( p <= RealType(1) ||
operator()(r, p - RealType(1)) ) );
110 template<
typename RealType,
class Random>
111 bool ExponentialProb::ExpFraction(
Random& r, RealType p)
const {
114 std::numeric_limits<word>::digits <
115 std::numeric_limits<RealType>::digits ?
116 std::numeric_limits<word>::digits :
117 std::numeric_limits<RealType>::digits;
119 unsigned m = 0, l = unsigned(_v.size());
122 if (p <= RealType(0))
126 _v.resize(l += alloc_incr);
127 _v[m++] = r.template Integer<word, c>();
128 p *= std::pow(RealType(2), c);
132 else if (_v[m - 1] < w)
139 for (
unsigned s = 0; ; s ^= 1) {
140 for (
unsigned j = 0; ; ++j) {
144 _v.resize(l += alloc_incr);
145 _v[m++] = r.template Integer<word, c>();
147 word w = r.template Integer<word, c>();
150 else if (w < _v[j]) {
162 #if defined(_MSC_VER)
163 # pragma warning (pop)
166 #endif // RANDOMLIB_EXPONENTIALPROB_HPP
The exponential probability.
Generate random integers, reals, and booleans.
#define STATIC_ASSERT(cond, reason)
bool operator()(Random &r, RealType p) const