std:: geometric_distribution
|
定义于头文件
<random>
|
||
|
template
<
class
IntType
=
int
>
class geometric_distribution ; |
(C++11 起) | |
生成随机非负整数值 i ,按照离散概率函数分布:
-
P(i|p) = p · (1 − p)
i
该值表示在一系列独立的是/否试验(每次试验成功的概率为 p)中,在恰好出现 1 次成功之前所经历的失败次数。
std :: geometric_distribution <> ( p ) 完全等价于 std:: negative_binomial_distribution <> ( 1 , p ) 。它也是 std::exponential_distribution 的离散对应版本。
std::geometric_distribution
满足
随机数分布
要求。
目录 |
模板参数
| IntType | - | 生成器产生的结果类型。若该类型不是 short 、 int 、 long 、 long long 、 unsigned short 、 unsigned int 、 unsigned long 或 unsigned long long 中的任意一种,则行为未定义。 |
成员类型
| 成员类型 | 定义 |
result_type
(C++11)
|
IntType |
param_type
(C++11)
|
参数集的类型,参见 RandomNumberDistribution 。 |
成员函数
|
(C++11)
|
构造新的分布
(公开成员函数) |
|
(C++11)
|
重置分布的内部状态
(公开成员函数) |
生成 |
|
|
(C++11)
|
生成分布中的下一个随机数
(公开成员函数) |
特性 |
|
|
(C++11)
|
返回
p
分布参数(试验生成
true
的概率)
(公开成员函数) |
|
(C++11)
|
获取或设置分布参数对象
(公开成员函数) |
|
(C++11)
|
返回可能生成的最小值
(公开成员函数) |
|
(C++11)
|
返回可能生成的最大值
(公开成员函数) |
非成员函数
|
(C++11)
(C++11)
(removed in C++20)
|
比较两个分布对象
(函数) |
|
(C++11)
|
对伪随机数分布执行流输入和输出操作
(函数模板) |
示例
std :: geometric_distribution <> ( 0.5 ) 是默认参数,表示获得一次正面朝上所需的硬币投掷次数。
#include <iomanip> #include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::mt19937 gen(rd()); std::geometric_distribution<> d; // 等同于 // std::negative_binomial_distribution<> d(1, 0.5): std::map<int, int> hist; for (int n = 0; n != 10000; ++n) ++hist[d(gen)]; for (auto [x, y] : hist) { const char c = x < 10 ? x + '0' : x - 10 + 'a'; std::cout << c << ' ' << std::string(y / 100, '*') << '\n'; } }
可能的输出:
0 ************************************************* 1 ************************* 2 ************ 3 ****** 4 ** 5 * 6 7 8 9
外部链接
| 韦斯坦,埃里克·W.《几何分布》 摘自 MathWorld —— 一个 Wolfram 网络资源。 |