C++
数字 | Numerics

std::uniform_int_distribution

STD:制服[医]INT[医]分布

Defined in header
template< class IntType = int > class uniform_int_distribution;(since C++11)

产生随机整数值i,在闭区间上均匀分布。[a, b],即按离散概率函数P%28i a,b%29=分布。

*。

B-a+1

...

std::uniform_int_distribution满足…的所有要求RandomNumberDistribution...

模板参数

IntType-The result type generated by the generator. The effect is undefined if this is not one of short, int, long, long long, unsigned short, unsigned int, unsigned long, or unsigned long long.

成员类型

Member typeDefinition
result_typeIntType
param_typethe type of the parameter set, see RandomNumberDistribution.

成员函数

(constructor)constructs new distribution (public member function)
resetresets the internal state of the distribution (public member function)

世代

运算符%28%29在分布%28公共成员函数%29中生成下一个随机数。

特征

AB返回分布参数%28公共成员函数%29

Param获取或设置分布参数对象%28公共成员函数%29

min返回最小潜在生成值%28公共成员函数%29

MAX返回最大潜在生成值%28公共成员函数%29

非会员职能

operator==operator!=compares two distribution objects (function)
operator<<operator>>performs stream input and output on pseudo-random number distribution (function template)

这个程序模拟掷六面骰子.

二次

#include <random> #include <iostream> int main() { std::random_device rd; //Will be used to obtain a seed for the random number engine std::mt19937 gen(rd() //Standard mersenne_twister_engine seeded with rd() std::uniform_int_distribution<> dis(1, 6 for (int n=0; n<10; ++n) //Use dis to transform the random unsigned int generated by gen into an int in [1, 6] std::cout << dis(gen) << ' '; std::cout << '\n'; }

二次

可能的产出:

二次

1 1 6 5 2 2 5 5 6 2

二次

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cpPreference.com/w/cpp/数值/随机/统一[医]INT[医]分布