C++11跨编译器/标准库随机分布的可重现性

9

尽管随机引擎需要在每个编译器上生成相同的数字序列,但至少一些随机分布并不要求如此,只需要满足统计和概率阈值即可。例如:

#include <random>
#include <iostream>

int main() {
  std::mt19937 foo;
  std::uniform_int_distribution<int> bar(0, 1000);

  for (int i=0; i<99; ++i) {
    bar(foo);
  }

  std::cout << bar(foo) << std::endl;

  return 0;
}

当编译使用(我版本的)libstdc++时,将打印808,而当编译使用libc++时,将打印89。

在任何兼容环境下,哪些标准提供的分布函数保证产生一致的结果?


6
相信我,89比808更加随机。;-) - Howard Hinnant
1
当然不像4那么随机。https://xkcd.com/221/ - OmnipotentEntity
感激您优雅地对待我的玩笑。+1 - Howard Hinnant
1个回答

6
很遗憾,截至N3936(C++14最终草案),没有一个标准的随机分布要求像这样。容易理解原因是有许多编写分布函数的有效方法,一些比其他更好,即使是基本的正态分布算法也在不断改进并成为活跃研究领域的主题。强制使用单个分布函数将不必要地阻碍未来算法的实现。
幸运的是,你可以自己编写分布函数。各种分布类的头文件规范位于§26.5.8下。但是,你的代码不一定需要遵循这个结构。
(请注意,我没有彻底测试此代码,并且可能会与某些引擎或溢出产生错误行为,尽管我已经尽力避免后者,但这仅仅是一个示例而不是权威的均匀分布源代码。话虽如此,如果你发现任何问题,请在评论中告诉我,我很乐意进行更正。)
#include <random>
#include <tuple>
#include <iostream>

template<class IntType = int>
class my_uniform_int_distribution {
public:
  // types
  typedef IntType result_type;
  typedef std::pair<int, int> param_type;

  // constructors and reset functions
  explicit my_uniform_int_distribution(IntType a = 0, IntType b = std::numeric_limits<IntType>::max());
  explicit my_uniform_int_distribution(const param_type& parm);
  void reset();

  // generating functions
  template<class URNG>
    result_type operator()(URNG& g);
  template<class URNG>
    result_type operator()(URNG& g, const param_type& parm);

  // property functions
  result_type a() const;
  result_type b() const;
  param_type param() const;
  void param(const param_type& parm);
  result_type min() const;
  result_type max() const;

private:
  typedef typename std::make_unsigned<IntType>::type diff_type;

  IntType lower;
  IntType upper;
};

template<class IntType>
my_uniform_int_distribution<IntType>::my_uniform_int_distribution(IntType a, IntType b) {
  param({a, b});
}

template<class IntType>
my_uniform_int_distribution<IntType>::my_uniform_int_distribution(const param_type& parm) {
  param(parm);
}

template<class IntType>
void my_uniform_int_distribution<IntType>::reset() {}

template<class IntType>
template<class URNG>
auto my_uniform_int_distribution<IntType>::operator()(URNG& g) -> result_type {
  return operator()(g, param());
}

template<class IntType>
template<class URNG>
auto my_uniform_int_distribution<IntType>::operator()(URNG& g, const param_type& parm) -> result_type {
  diff_type diff = (diff_type)parm.second - (diff_type)parm.first + 1;
  if (diff == 0) // If the +1 overflows we are using the full range, just return g()
    return g();

  diff_type badDistLimit = std::numeric_limits<diff_type>::max() / diff;
  do {
    diff_type generatedRand = g();

    if (generatedRand / diff < badDistLimit)
      return (IntType)((generatedRand % diff) + (diff_type)parm.first);
  } while (true);
}

template<class IntType>
auto my_uniform_int_distribution<IntType>::a() const -> result_type {
  return lower;
}

template<class IntType>
auto my_uniform_int_distribution<IntType>::b() const -> result_type {
  return upper;
}

template<class IntType>
auto my_uniform_int_distribution<IntType>::param() const -> param_type {
  return {lower, upper};
}

template<class IntType>
void my_uniform_int_distribution<IntType>::param(const param_type& parm) {
  std::tie(lower, upper) = parm;
  if (upper < lower)
    throw std::exception();
}

template<class IntType>
auto my_uniform_int_distribution<IntType>::min() const -> result_type {
  return lower;
}

template<class IntType>
auto my_uniform_int_distribution<IntType>::max() const -> result_type {
  return upper;
}

int main() {
  std::mt19937 foo;
  my_uniform_int_distribution<int> bar(0,1000);

  for (int i=0; i<99; ++i) {
    bar(foo);
  }

  std::cout << bar(foo) << std::endl;

  return 0;
}

这段代码在我测试过的所有平台上都会输出490。

1
N3797不是C++14的最终草案。N3936才是。 - T.C.
@T.C.:从技术上讲,N4141(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4139.html)与之间的区别仅在于少量编辑修复。而且这些修复都不是公开可用的。 :-( - Howard Hinnant
这段代码非常接近正确,但它假设 URNG::max() == std::numeric_limits<diff_type>::max()URNG::min() == 0。一些 PRNG 引擎的最小值为 1。 - MtnViewJohn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接