std::independent_bits_engine<Engine,W,UIntType>::independent_bits_engine

来自cppreference.com

 
 
 
 
 
independent_bits_engine();
(1) (C++11 起)
explicit independent_bits_engine( result_type s );
(2) (C++11 起)
template< class Sseq >
explicit independent_bits_engine( Sseq& seq );
(3) (C++11 起)
explicit independent_bits_engine( const Engine& e );
(4) (C++11 起)
explicit independent_bits_engine( Engine&& e );
(5) (C++11 起)

构造新的伪随机数引擎适配器。

1) 默认构造函数。亦默认构造底层引擎。
2)s 构造底层引擎
3) 以种子序列 seq 构造底层引擎。 此构造函数仅若 Sseq 满足种子序列 (SeedSequence) 才参与重载决议。特别是若 Sseq 可隐式转换为 result_type 则此构造函数不参与重载决议。
4)e 的副本构造底层引擎。
5)e 移动构造底层引擎。之后 e 保有未指定但合法的状态。

参数

s - 用以构造底层引擎的整数值
seq - 用以构造底层引擎的种子序列
e - 用以初始化的伪随机数引擎

示例

#include <iostream>
#include <random>
 
int main()
{
    auto print = [](auto rem, auto engine, int count)
    {
        std::cout << rem << ": ";
        for (int i {}; i != count; ++i)
            std::cout << static_cast<unsigned>(engine()) << ' ';
        std::cout << '\n';
    };
 
    std::independent_bits_engine<std::mt19937, /*bits*/ 1, unsigned short>
        e1; // 默认构造
    print("e1", e1, 8);
 
    std::independent_bits_engine<std::mt19937, /*bits*/ 1, unsigned int>
        e2(1); // 以 l 构造
    print("e2", e2, 8);
 
    std::random_device rd;
    std::independent_bits_engine<std::mt19937, /*bits*/ 3, unsigned long>
        e3(rd()); // 以 rd() 播种
    print("e3", e3, 8);
 
    std::seed_seq s {3, 1, 4, 1, 5};
    std::independent_bits_engine<std::mt19937, /*bits*/ 3, unsigned long long>
        e4(s); // 以种子序列 s 播种
    print("e4", e4, 8);
}

可能的输出:

e1: 0 0 0 1 0 1 1 1
e2: 1 1 0 0 1 1 1 1
e3: 3 1 5 4 3 2 3 4
e4: 0 2 4 4 4 3 3 6