简体   繁体   English

uniform_int_distribution a(),b(),min()和max()

[英]uniform_int_distribution a(), b(), min(), and max()

http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution/params http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution/min http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution/max http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution/params http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution/min http://en.cppreference。 COM / W / CPP /数字/随机/ uniform_int_distribution / MAX

It looks like member function a() is equivalent to member function min() , and member function b() is equivalent to max() . 看起来成员函数a()等价于成员函数min() ,而成员函数b()等同于max()

#include <iostream>
#include <random>

int main() {
    std::uniform_int_distribution<int> dist(5, 10);
    std::cout << "a " << dist.a() << '\n';
    std::cout << "b " << dist.b() << '\n';
    std::cout << "min " << dist.min() << '\n';
    std::cout << "max " << dist.max() << '\n';
}

prints 版画

a 5
b 10
min 5
max 10

When compiled using gcc's standard library. 使用gcc的标准库编译时。 Are these functions really identical, and if so, why are a() and b() defined? 这些函数是否真的相同,如果是,为什么定义a()b()

Are these functions really identical 这些功能是否完全相同

They are identical, because it is reasonable and practical to always assume so. 它们是相同的,因为总是假设它是合理和实际的。 It would be true but pedantic and misleading to say it is not an absolute guarantee. 如果说这不是绝对的保证,那将是真实的但是迂腐和误导。

I only have access to MinGW 4.7.1, which I assume its standard library is the same as GCC one. 我只能访问MinGW 4.7.1,我认为它的标准库与GCC一样。 Inside it, the class template uniform_int_distribution has members: 在其中,类模板uniform_int_distribution具有成员:

  /**
   * @brief Returns the inclusive lower bound of the distribution range.
   */
  result_type
  min() const
  { return this->a(); }

  /**
   * @brief Returns the inclusive upper bound of the distribution range.
   */
  result_type
  max() const
  { return this->b(); }

So, after function inlining, ( a and min ) and ( b and max ) should be tanslated into identical code. 因此,在函数内联之后,( amin )和( bmax )应该被转换为相同的代码。

Reading the standard section 26.5.8.2.1, you will find that it (only indirectly) says they should return the same value. 阅读标准部分26.5.8.2.1,您会发现(仅间接地)它们应该返回相同的值。 As a result, a sane library implementer will make them effectively identical or at least not-so-different. 因此,一个理智的库实现者将使它们实际上相同或至少不那么不同。


why are a() and b() defined? 为什么定义a()和b()?

I can only guess. 我只能猜测。 It may be about consistency, but the consistency is in a less formal sense. 它可能与一致性有关,但一致性不太正式。

Mathematically, a uniform distribution is like this: 在数学上,均匀分布是这样的:

P(i|a,b) = 1/(b-a+1)

And in uniform_int_distribution , we have uniform_int_distribution ,我们有

uniform_int_distribution::a()
uniform_int_distribution::b()

For Bernoulli distribution and bernoulli_distribution : 对于伯努利分布和bernoulli_distribution

P(i|p) = [complicated]

bernoulli_distribution::p()

Poisson: 泊松:

P(i|mean) = [complicated]

poisson_distribution::mean()

Normal: 正常:

P(x|mean, standard-deviation) = [complicated]

normal_distribution::mean()
normal_distribution::stddev()

We can observe that they all tell their parameters. 我们可以观察到他们都告诉他们的参数。 It is not useful to generic code, but that may be helpful in some situations. 它对通用代码没有用,但在某些情况下可能会有所帮助。

a() and b() return distribution parameters while min() and max() return minimum and maximum potentially generated value. a()b()返回分布参数,而min()max()返回最小和最大可能生成的值。 For the uniform distribution the value returned by min() and a() are equal, and same for max() and b() . 对于均匀分布, min()a()返回的值相等, max()b()返回的值相同。 In general, for other distributions there may not be such correspondence, so I guess a() and b() are there for consistency. 一般来说,对于其他发行版可能没有这样的对应关系,所以我猜a()b()是为了保持一致性。

Every random number distribution D must have methods 每个随机数分布D必须有方法

D::result_type D::min();
D::result_type D::max();

which return the "greatest lower bound and the least upper bound on the values potentially returned by d 's operator() , as determined by the current values of d 's parameters" (§25.1.6, [rand.req.dist] ; quote is from paragraph 3(d); the requirement is in Table 117). 其返回“最大下界和上通过潜在地返回的值的最小上界doperator()如通过的电流值来确定d的参数”(§25.1.6, [rand.req.dist] ;引自第3(d)段;要求见表117)。

Also, there is an expectation that a distribution D and its associated parameter type D::param_type (called P in the following quote) will have corresponding constructors and parameter accessors. 此外,期望分布D及其相关参数类型D::param_type (在以下引用中称为P )将具有对应的构造函数和参数访问器。 Paragraph 9 of the same section: 同一段第9段:

For each of the constructors of D taking arguments corresponding to parameters of the distribution, P shall have a corresponding constructor subject to the same requirements and taking arguments identical in number, type, and default values. 对于D参数的每个构造函数,该参数对应于分布的参数, P应具有相应的构造函数,这些构造函数具有相同的要求并且在数量,类型和默认值方面具有相同的参数。 Moreover, for each of the member functions of D that return values corresponding to parameters of the distribution, P shall have a corresponding member function with the identical name, type, and semantics. 此外,对于返回对应于分布参数的值的D每个成员函数, P应具有相同的成员函数,其具有相同的名称,类型和语义。

So std::uniform_int_distribution::min and std::uniform_int_distribution::max are the bounds of a particular distribution instance's possible return values, while std::uniform_int_distribution::a and std::uniform_int_distribution::b are the parameter values which constructed that particular instance. 所以std::uniform_int_distribution::minstd::uniform_int_distribution::max是特定分布实例的可能返回值的边界,而std::uniform_int_distribution::astd::uniform_int_distribution::b是构造的参数值那个特定的例子。 As it happens, in the particular case of std::uniform_{int,real}_distribution , the parameters precisely correspond to the bounds, but the requirements suggest that both bounds and parameters should be provided. 碰巧,在std::uniform_{int,real}_distribution的特定情况下,参数精确地对应于边界,但是要求表明应该提供边界和参数。

I suppose it would have been possible to also call the parameter accessors min and max , but the committee chose not to do that. 我想也可以调用参数访问器minmax ,但委员会选择不这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM