std::independent_bits_engine<Engine,W,UIntType>::independent_bits_engine
来自cppreference.com
< cpp | numeric | random | independent bits engine
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) 的要求时才会参与重载决议。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
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2181 | C++11 | 即使 seq.generate 调用抛出了异常,重载 (3) 也不会抛出异常 | 会传播该异常 |