std::bernoulli_distribution
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <random>
  | 
||
|   class bernoulli_distribution;  | 
(C++11 起) | |
根据离散概率函数产生随机布尔值。 true 的概率为
- P(b|p) =⎧
⎨
⎩p if b == true
1 − p if b == false 
std::bernoulli_distribution 满足随机数分布 (RandomNumberDistribution) 。
成员类型
| 成员类型 | 定义 | 
  result_type
 | 
bool | 
  param_type
 | 
参数集的类型,见随机数分布 (RandomNumberDistribution) 。 | 
成员函数
|   构造新分布  (公开成员函数)  | |
|   重置分布的内部状态  (公开成员函数)  | |
 生成 | |
|   生成分布中的下个随机数  (公开成员函数)  | |
 特征 | |
|   返回 p 分布参数(生成 true 的概率)  (公开成员函数)  | |
|   获取或设置随机参数对象  (公开成员函数)  | |
|   返回最小的潜在生成值  (公开成员函数)  | |
|   返回最大的潜在生成值  (公开成员函数)  | |
非成员函数
|   比较两个分布对象  (函数)  | |
|   执行伪随机数分布的流输入和输出  (函数模板)  | 
示例
运行此代码
#include <iostream> #include <iomanip> #include <string> #include <map> #include <random> int main() { std::random_device rd; std::mt19937 gen(rd()); // 1/4 时间给出 "true" // 3/4 时间给出 "false" std::bernoulli_distribution d(0.25); std::map<bool, int> hist; for(int n=0; n<10000; ++n) { ++hist[d(gen)]; } for(auto p : hist) { std::cout << std::boolalpha << std::setw(5) << p.first << ' ' << std::string(p.second/500, '*') << '\n'; } }
可能的输出:
false *************** true ****
外部链接
Weisstein, Eric W. “伯努利分布。”来自 MathWorld--A Wolfram Web Resource 。