[rand.adapt.ibits]

# 29 Numerics library [[numerics]](./#numerics)

## 29.5 Random number generation [[rand]](rand#adapt.ibits)

### 29.5.5 Random number engine adaptor class templates [[rand.adapt]](rand.adapt#ibits)

#### 29.5.5.3 Class template independent_bits_engine [rand.adapt.ibits]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L3550)

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[.](#1.sentence-1)

The state xi of an independent_bits_engine engine adaptor object x consists of
 the state ei of its base engine e;
the size of the state is
 the size of e's state[.](#1.sentence-2)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L3565)

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

- [(2.1)](#2.1)

  Let R=e.max() - e.min() + 1 and m=âlog2Râ[.](#2.1.sentence-1)

- [(2.2)](#2.2)

  With n as determined below,
 let w0=âw/nâ, n0=nâwmodn, y0=2w0âR/2w0â,
 and y1=2w0+1âR/2w0+1â[.](#2.2.sentence-1)

- [(2.3)](#2.3)

  Let n=âw/mâ if and only if the relation Rây0â¤ây0/nâ holds as a result[.](#2.3.sentence-1)
  Otherwise let n=1+âw/mâ[.](#2.3.sentence-2)

[*Note [1](#note-1)*:

The relation w=n0w0+(nân0)(w0+1) always holds[.](#2.sentence-2)

â *end note*]

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L3598)

The transition algorithm
is carried out by invoking e() as often as needed to obtain n0 values less than y0+e.min() and nân0 values less than y1+e.min()[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L3605)

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â n0; k += 1) {do u = e() - e.min(); while (uâ¥y0); S = 2w0âS+umod2w0;}for (k = n0; k â n; k += 1) {do u = e() - e.min(); while (uâ¥y1); S = 2w0+1âS+umod2w0+1;}

[ð](#lib:independent_bits_engine_)

namespace std {template<class Engine, size_t w, class UIntType>class independent_bits_engine {public:// typesusing result_type = UIntType; // engine characteristicsstatic constexpr result_type min() { return 0; }static constexpr result_type max() { return 2wâ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); // equality operatorsfriend bool operator==(const independent_bits_engine& x, const independent_bits_engine& y); // generating functions result_type operator()(); void discard(unsigned long long z); // property functionsconst Engine& base() const noexcept { return e; }// inserters and extractorstemplate<class charT, class traits>friend basic_ostream<charT, traits>&operator<<(basic_ostream<charT, traits>& os, const independent_bits_engine& x); // hostedtemplate<class charT, class traits>friend basic_istream<charT, traits>&operator>>(basic_istream<charT, traits>& is, independent_bits_engine& x); // hostedprivate: Engine e; // *exposition only*};}

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L3671)

The following relations shall hold: 0 < w and w <= numeric_limits<result_type>​::​digits[.](#5.sentence-1)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L3677)

The textual representation
consists of the textual representation of e[.](#6.sentence-1)
