我可以更改分配参数吗? [英] Can I change a distribution parameters?
问题描述
<随机> 创建该对象时,我要定义一个时间间隔. 创建后可以更改此间隔吗?
There is uniform_int_distribution in < random > When I creating that I define an interval. Can I change this interval after the creation?
例如
std::uniform_int_distribution distr(0, 10);
// can I change an interval here ?
推荐答案
只需为变量分配新的分布:
Just assign a new distribution to the variable:
std::uniform_int_distribution<int> distr(0, 10);
distr = std::uniform_int_distribution<int>(5, 13);
或者,为此创建一个参数(@awesomeyi回答需要创建分发对象,但这仍然需要param_type对象创建)
Or, create a parameter for that (@awesomeyi answer required distribution object creation, this still requires param_type object creation)
std::uniform_int_distribution<int> distr(0, 10);
distr.param(std::uniform_int_distribution<int>::param_type(5, 13));
证明param_type可以工作(对于@stefan):
Proof that param_type will work (for @stefan):
P是关联的param_type.它应满足 CopyConstructible,CopyAssignable和EqualityComparable 要求.它还具有采用以下相同参数的构造函数: 与D的构造函数相同的类型,并具有成员函数 与D的参数返回获取器相同
P is the associated param_type. It shall satisfy the CopyConstructible, CopyAssignable, and EqualityComparable requirements. It also has constructors that take the same arguments of the same types as the constructors of D and has member functions identical to the parameter-returning getters of D
http://en.cppreference.com/w/cpp/concept/RandomNumberDistribution
这篇关于我可以更改分配参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!