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) |
构造新的分布 (public member function) |
(C++11) |
重置分布的内部状态 (public member function) |
生成 | |
(C++11) |
生成分布中的下一个随机数 (public member function) |
特性 | |
(C++11) |
返回分布参数 (public member function) |
(C++11) |
获取或设置分布参数对象 (public member function) |
(C++11) |
返回可能生成的最小值 (public member function) |
(C++11) |
返回可能生成的最大值 (public member function) |
[编辑] 非成员函数
(C++11起)(C++11起)(C++20中移除) |
比较两个分布对象 (function) |
(C++11) |
对伪随机数分布执行流输入和输出 (function template) |
[编辑] 示例
运行此代码
#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." From MathWorld — A Wolfram Web Resource. |