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

貌似成员函数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';
}

打印

a 5
b 10
min 5
max 10

使用 gcc 的标准库编译时。这些功能真的相同吗?如果是,为什么要定义 a()b()

a()b() return 分布参数,而 min()max() return 最小和最大可能生成的值。对于均匀分布,由 min()a() 计算的值 return 相等,max()b() 也相同。一般来说,对于其他分布可能没有这样的对应关系,所以我猜 a()b() 是为了保持一致性。

每个随机数分布D必须有方法

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

其中 return "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];引用来自第 3(d) 段;要求在 Table 117 中)。

此外,期望分布 D 及其关联的参数类型 D::param_type(在下面的引用中称为 P)将具有相应的构造函数和参数访问器。同节第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. 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.

所以 std::uniform_int_distribution::minstd::uniform_int_distribution::max 是特定分布实例的可能 return 值的边界,而 std::uniform_int_distribution::astd::uniform_int_distribution::b 是参数值构建了那个特定的实例。碰巧的是,在 std::uniform_{int,real}_distribution 的特定情况下,参数恰好对应于边界,但要求表明应同时提供边界和参数。

我想也可以调用参数访问器 minmax,但委员会选择不这样做。

Are these functions really identical

它们是相同的,因为总是这样假设是合理和实际的。说这不是绝对保证是对的,但迂腐和误导。

我只能访问 MinGW 4.7.1,我认为它的标准库与 GCC 相同。在其中,class 模板 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(); }

因此,在函数内联之后,(a and min) 和 (b and max) 应该被转换成相同的代码。

阅读标准部分 26.5.8.2.1,您会发现它(只是间接地)说它们应该 return 相同的值。因此,一个理智的库实现者将使它们有效地相同或至少没有那么不同。


why are a() and b() defined?

我只能猜测。这可能是关于一致性,但一致性是在不太正式的意义上。

在数学上,均匀分布是这样的:

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

uniform_int_distribution中,我们有

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

对于伯努利分布和 bernoulli_distribution:

P(i|p) = [complicated]

bernoulli_distribution::p()

泊松分布:

P(i|mean) = [complicated]

poisson_distribution::mean()

正常:

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

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

我们可以观察到他们都说出了自己的参数。它对通用代码没有用,但在某些情况下可能会有帮助。