14 #ifndef __INCLUDED__SCOPIRA_TOOL_DISTRANDOM_H__ 15 #define __INCLUDED__SCOPIRA_TOOL_DISTRANDOM_H__ 17 #include <scopira/tool/limits.h> 18 #include <scopira/tool/linconrandom.h> 24 template <
class UniRanGen,
class RealType =
double>
26 template <
class UniRanGen,
class RealType =
double>
28 template <
class UniRanGen,
class IntType =
int>
30 template<
class UniRanGen,
class IntType =
int>
56 template <
class UniRanGen,
class RealType>
60 typedef UniRanGen base_type;
61 typedef RealType result_type;
62 static const bool has_fixed_range =
true;
68 result_type
min()
const {
return 0.0; }
70 result_type
max()
const {
return 1.0; }
72 result_type operator()(
void) {
73 return static_cast<result_type
>(m_rng() - m_rng.min()) /
74 (static_cast<result_type>(m_rng.max()-m_rng.min()) +
75 (std::numeric_limits<base_result>::is_integer ? 1.0 : 0.0));
79 typedef typename base_type::result_type base_result;
88 template <
class UniRanGen,
class RealType>
92 typedef UniRanGen base_type;
93 typedef RealType result_type;
94 static const bool has_fixed_range =
false;
98 : m_rng(rng), m_min(min), m_max(max) { }
101 result_type
min(
void)
const {
return m_min; }
103 result_type
max(
void)
const {
return m_max; }
105 result_type operator()() {
return m_rng() * (m_max - m_min) + m_min; }
109 RealType m_min, m_max;
117 template <
class UniRanGen,
class IntType>
121 typedef UniRanGen base_type;
122 typedef IntType result_type;
123 static const bool has_fixed_range =
false;
130 result_type
min(
void)
const {
return m_min; }
132 result_type
max(
void)
const {
return m_max; }
137 return long((m_rng() - m_rng.min()) / m_factor) % long(m_range) + m_min;
141 typedef typename base_type::result_type base_result;
143 IntType m_min, m_max;
149 template <
class UniRanGen,
class IntType>
151 (base_type & rng, IntType
min, IntType
max)
152 : m_rng(rng), m_min(min), m_max(max),
153 m_range(static_cast<base_result>(m_max-m_min)+1), m_factor(1)
161 base_result r_base = m_rng.max() - m_rng.min();
162 if (r_base == std::numeric_limits<base_result>::max()) {
167 if(
long(r_base) %
long(m_range) == 0) {
169 m_factor = int(
double(r_base) /
double(m_range));
175 for ( ; r_base/m_range/32 >= m_range; m_factor *= 2)
180 template<
class UniRanGen,
class IntType>
184 typedef UniRanGen base_type;
185 typedef IntType input_type;
186 typedef IntType result_type;
187 static const bool has_fixed_range =
false;
189 explicit uniint_dist(UniRanGen &rng, IntType min, IntType max)
190 : m_rng(rng), m_min(min), m_max(max) { m_range = m_max - m_min; }
193 result_type
min(
void)
const {
return m_min; }
195 result_type
max(
void)
const {
return m_max; }
198 result_type operator()(
void);
201 typedef typename base_type::result_type base_result;
203 IntType m_min, m_max, m_range;
206 template<
class UniRanGen,
class IntType>
209 base_result bmin = (m_rng.min)();
210 base_result brange = (m_rng.max)() - (m_rng.min)();
214 }
else if (brange == m_range) {
217 return static_cast<result_type
>(m_rng() - bmin) + m_min;
218 }
else if (brange < m_range) {
224 if(m_range == (std::numeric_limits<result_type>::max)()) {
225 limit = m_range/(result_type(brange)+1);
226 if(m_range % result_type(brange)+1 == result_type(brange))
229 limit = (m_range+1)/(result_type(brange)+1);
233 result_type result = result_type(0);
234 result_type mult = result_type(1);
235 while(mult <= limit) {
236 result += (m_rng() - bmin) * mult;
237 mult *= result_type(brange)+result_type(1);
244 if(result <= m_range)
245 return result + m_min;
248 if (brange / m_range > 4 ) {
255 base_result result = m_rng() - bmin;
258 if(result <= static_cast<base_result>(m_range))
259 return result + m_min;
Definition: archiveflow.h:20