An independent_bits_engine
random number engine adaptor
combines random numbers
that are produced by some base engine e,
so as to produce random numbers
with a specified number of bits w.
The state
x_{i}
of an independent_bits_engine
engine adaptor object x
consists of
the state e_{i} of its base engine e;
the size of the state is
the size of e's state.

The transition and generation algorithms are described in terms of the following integral constants:

Let R = e.max() - e.min() + 1 and m = ⌊ log

_{2}R ⌋ .With n as determined below, let w

_{0}= ⌊ w / n ⌋ , n_{0}= n - w mod n , y_{0}= 2^{w0}⌊ R / 2^{w0}⌋ , and y_{1}= 2^{w0 + 1}⌊ R / 2^{w0 + 1}⌋ .Let n = ⌈ w / m ⌉ if and only if the relation R - y

_{0}≤ ⌊ y_{0}/ n ⌋ holds as a result. Otherwise let n = 1 + ⌈ w / m ⌉ .

[ *Note:*
The relation
w = n_{0} w_{0} + (n - n_{0})(w_{0} + 1)
always holds.
* — end note* ]

The transition algorithm
is carried out
by invoking e()
as often as needed
to obtain
n_{0}
values less than
y_{0} + e.min()
and
n - n_{0}
values less than y_{1} + e.min() .

The generation algorithm uses the values produced while advancing the state as described above to yield a quantity S obtained as if by the following algorithm:

S = 0; for (k = 0; k ≠ n_{0}; k += 1) { do u = e() - e.min(); while ( u ≥ y_{0}); S = 2^{w0}· S + u mod 2^{w0}; } for (k = n_{0}; k ≠ n; k += 1) { do u = e() - e.min(); while ( u ≥ y_{1}); S = 2^{w0 + 1}· S + u mod 2^{w0 + 1}; }

template<class Engine, size_t w, class UIntType> class independent_bits_engine{ public: // types typedef UIntType result_type; // engine characteristics static constexpr result_type min() { return 0; } static constexpr result_type max() { return 2^{w}- 1; } // constructors and seeding functions independent_bits_engine(); explicit independent_bits_engine(const Engine& e); explicit independent_bits_engine(Engine&& e); explicit independent_bits_engine(result_type s); template<class Sseq> explicit independent_bits_engine(Sseq& q); void seed(); void seed(result_type s); template<class Sseq> void seed(Sseq& q); // generating functions result_type operator()(); void discard(unsigned long long z); // property functions const Engine& base() const noexcept { return e; }; private: Engine e; //exposition only};

The following relations shall hold: 0 < w and w <= numeric_limits<result_type>::digits.