std::gamma_distribution
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   在标头  <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()); // alpha=1,beta=2 的 gamma 分布近似于指数分布。 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. “Γ 分布。”来自 MathWorld--A Wolfram Web Resource。 |