std::gamma_distribution
来自 cppreference.cn
定义于头文件 <random> |
||
template< class RealType = double > class gamma_distribution; |
(自 C++11 起) | |
产生随机正浮点数值 x,根据概率密度函数分布
- P(x|α,β) =
· xα-1e-x/β βα
· Γ(α)
其中 α 被称为形状参数,β 被称为尺度参数。形状参数有时用字母 k 表示,尺度参数有时用字母 θ 表示。
对于浮点数 α,获得的值是 α 个独立指数分布随机变量的总和,每个变量的平均值为 β。
std::gamma_distribution
满足 RandomNumberDistribution 随机数分布 的要求。
目录 |
[编辑] 模板形参
RealType | - | 生成器产生的结果类型。如果不是 float、 double 或 long double 之一,则效果未定义。 |
[编辑] 成员类型
成员类型 | 定义 |
result_type (C++11) |
RealType |
param_type (C++11) |
参数集类型,见 RandomNumberDistribution 随机数分布。 |
[编辑] 成员函数
(C++11) |
构造新的分布 (公有成员函数) |
(C++11) |
重置分布的内部状态 (公有成员函数) |
生成 | |
(C++11) |
生成分布中的下一个随机数 (公有成员函数) |
特性 | |
(C++11) |
返回分布参数 (公有成员函数) |
(C++11) |
获取或设置分布参数对象 (公有成员函数) |
(C++11) |
返回可能生成的最小值 (公有成员函数) |
(C++11) |
返回可能生成的最大值 (公有成员函数) |
[编辑] 非成员函数
(C++11)(C++11)(C++20 中移除) |
比较两个分布对象 (函数) |
(C++11) |
对伪随机数分布执行流输入和输出 (函数模板) |
[编辑] 示例
运行此代码
#include <iomanip> #include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::mt19937 gen(rd()); // A gamma distribution with alpha = 1, and beta = 2 // approximates an exponential distribution. std::gamma_distribution<> d(1, 2); std::map<int, int> hist; for (int n = 0; n != 10000; ++n) ++hist[2 * d(gen)]; for (auto const& [x, y] : hist) if (y / 100.0 > 0.5) std::cout << std::fixed << std::setprecision(1) << x / 2.0 << '-' << (x + 1) / 2.0 << ' ' << std::string(y / 100, '*') << '\n'; }
可能的输出
0.0-0.5 ********************** 0.5-1.0 **************** 1.0-1.5 ************* 1.5-2.0 ********** 2.0-2.5 ******** 2.5-3.0 ****** 3.0-3.5 ***** 3.5-4.0 **** 4.0-4.5 *** 4.5-5.0 ** 5.0-5.5 ** 5.5-6.0 * 6.0-6.5 * 6.5-7.0 7.0-7.5 7.5-8.0
[编辑] 外部链接
Weisstein, Eric W. "Gamma Distribution." 来自 MathWorld — Wolfram Web 资源。 |