std::exponential_distribution
来自 cppreference.cn
定义于头文件 <random> |
||
template< class RealType = double > class exponential_distribution; |
(C++11 起) | |
生成服从以下概率密度函数的非负浮点随机值 x:
- P(x|λ) = λe-λx
如果随机事件以每单位时间/距离 λ 的恒定速率发生,则获得的值是直到下一个随机事件发生的时间/距离。例如,此分布描述了盖革计数器两次点击之间的时间,或 DNA 链中点突变之间的距离。
这是 std::geometric_distribution 的连续对应物。
std::exponential_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) |
返回 lambda 分布参数(事件速率) (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) |
[编辑] 注意
某些实现可能会在 RealType
为 float 时偶尔返回无穷大。这是 LWG issue 2524。
[编辑] 示例
运行此代码
#include <iomanip> #include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::mt19937 gen(rd()); // if particles decay once per second on average, // how much time, in seconds, until the next one? std::exponential_distribution<> d(1); std::map<int, int> hist; for (int n = 0; n != 10000; ++n) ++hist[2 * d(gen)]; for (auto const& [x, y] : hist) std::cout << std::fixed << std::setprecision(1) << x / 2.0 << '-' << (x + 1) / 2.0 << ' ' << std::string(y / 200, '*') << '\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
[编辑] 外部链接
Weisstein, Eric W. "指数分布." 来自 MathWorld — Wolfram Web Resource。 |