10 #if !defined(EXRANDOM_I_RAND_HPP) 11 #define EXRANDOM_I_RAND_HPP 1 44 template<
typename digit_gen>
class i_rand {
55 : _a(0), _d(1), _l(0), _D(D) {}
66 template<
typename Generator>
69 for (
long long v = 1, c = 0;;) {
71 for (
long long w = v, a = c, d = 1;;) {
77 long long j = (a / m) * m; a -= j; w -= j;
79 if (a + d <= m) { _a = int(a); _d = int(d);
return *
this; }
83 w *= b; a *= b; d *= b; ++_l;
85 long long j = (v / m) * m; v -= j; c -= j;
86 v *= b; c *= b; c += _D(g);
96 template<
typename Generator>
98 {
while (_l)
refine(g);
return _a; }
102 int min()
const {
return _a; }
106 int max()
const {
return _a + _d - 1; }
120 void add(
int c) { _a += c; }
132 template<
typename Generator>
133 bool less_than(Generator& g,
long long m,
long long n = 1) {
135 if ( (n *
max() < m))
return true;
136 if (!(n *
min() < m))
return false;
151 template<
typename Generator>
165 template<
typename Generator>
179 template<
typename Generator>
190 std::ostringstream os;
192 os <<
min() <<
"+[0," <<
max() -
min() + 1 <<
')';
203 template<
typename Generator>
205 if (_l > 0) { --_l; _d /= b; _a += _D(g) * _d; }
217 { os << h.
print();
return os; }
220 static const int b = digit_gen::base;
229 #endif // EXRANDOM_I_RAND_HPP
bool greater_than(Generator &g, long long m, long long n=1)
std::string print() const
A class to sample integers [0,m).
friend std::ostream & operator<<(std::ostream &os, const i_rand &h)
int operator()(Generator &g)
void refine(Generator &g)
i_rand & init(Generator &g, int m)
bool less_than_equal(Generator &g, long long m, long long n=1)
bool greater_than_equal(Generator &g, long long m, long long n=1)
bool less_than(Generator &g, long long m, long long n=1)