std::piecewise_linear_distribution
来自 cppreference.cn
定义于头文件 <random> |
||
template< class RealType = double > class piecewise_linear_distribution; |
(自 C++11 起) | |
std::piecewise_linear_distribution
生成随机浮点数,这些数字根据多个子区间 [bi, bi+1) 内的分段线性概率密度函数分布。分布方式使得每个区间边界的概率密度恰好是预定义的值 pi。
bi+1-x |
bi+1-bi |
x-bi |
bi+1-bi |
1 |
2 |
区间边界集 bi 和边界权重集 wi 是此分布的参数。
std::piecewise_linear_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()}; // increase the probability from 0 to 5 // remain flat from 5 to 10 // decrease from 10 to 15 at the same rate std::vector<double> i{0, 5, 10, 15}; std::vector<double> w{0, 1, 1, 0}; std::piecewise_linear_distribution<> d{i.begin(), i.end(), w.begin()}; std::map<int, int> hist; for (int n{}; n < 1e4; ++n) ++hist[d(gen)]; for (auto [x, y] : hist) std::cout << std::setw(2) << std::setfill('0') << x << ' ' << std::string(y / 100, '*') << '\n'; }
可能的输出
00 * 01 *** 02 **** 03 ****** 04 ********* 05 ********* 06 ********* 07 ********** 08 ********* 09 ********** 10 ********* 11 ******* 12 **** 13 *** 14 *