stl随机分布和可移植性 [英] stl random distributions and portability

查看:144
本文介绍了stl随机分布和可移植性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么标准分布的结果不是强制要求跨实现一致?

Why is it that the result of standard distributions isn't mandated to be consistent across implementations? The result of pseudo random number generators is on the other hand mandated to be identical.

例如,下面几乎肯定会打印出不同的东西

For example, the following will almost certainly print something different for every different standard library implementation.

std::mt19937 random {100};
std::normal_distribution<> dist;

std::cout << dist(random);

说我想做程序生成,并想要相同的起始种子,编译器。我不能用stl做。我必须回归到使用boost。为什么这不是一个缺陷?

Say I want to do procedural generation and would like identical starting seeds to result in identical results across platforms and compilers. I can't do it with the stl. I have to "regress" to using boost. Why isn't this a defect?

推荐答案

这不是缺陷,有关这方面的基本原理,请参阅有关添加可扩展随机数的提案设施到标准图书馆(N1398),说明(强调我):

This is not a defect, it is by design. The rationale for this can be found in A Proposal to Add an Extensible Random Number Facility to the Standard Library (N1398) which says (emphasis mine):


另一方面,分布式的规范只有
定义了统计结果,而不是精确的算法使用。这个
不同于引擎,因为对于分布算法,
严格证明它们的正确性是可用的,通常在
前提条件下,输入随机数(真)均匀地分布
。例如,至少有一些算法
已知从均匀分布的
生成正态分布的随机数
哪一个是最有效的取决于
的最小值各种超然的
函数,CPU的缓存和分支预测行为的相对执行速度,以及
所需的内存使用强>。因此,该提议使得对实现选择
算法。 遵循
的输出序列在实现之间的分布将不相同
。它是
预计实现会仔细选择的
分布的算法,因为它肯定是令人惊讶的客户
如果一些分布产生不同的数字从一个
实现版本到

On the other hand, the specifications for the distributions only define the statistical result, not the precise algorithm to use. This is different from engines, because for distribution algorithms, rigorous proofs of their correctness are available, usually under the precondition that the input random numbers are (truely) uniformly distributed. For example, there are at least a handful of algorithms known to produce normally distributed random numbers from uniformly distributed ones. Which one of these is most efficient depends on at least the relative execution speeds for various transcendental functions, cache and branch prediction behaviour of the CPU, and desired memory use. This proposal therefore leaves the choice of the algorithm to the implementation. It follows that output sequences for the distributions will not be identical across implementations. It is expected that implementations will carefully choose the algorithms for distributions up front, since it is certainly surprising to customers if some distribution produces different numbers from one implementation version to the next.

这一点在实现定义部分中重复说明:

This point is reiterated in the implementation defined section which says:

如何产生各种分布的算法被指定为
作为实现定义,因为每个分布都有各种各样的
算法。每个在速度,适应最近的计算机体系结构和
内存使用方面有不同的权衡
。该实现需要记录其选择,因此
,用户可以判断它是否可以接受质量。

The algorithms how to produce the various distributions are specified as implementation-defined, because there is a vast variety of algorithms known for each distribution. Each has a different trade-off in terms of speed, adaptation to recent computer architectures, and memory use. The implementation is required to document its choice so that the user can judge whether it is acceptable quality-wise.

这篇关于stl随机分布和可移植性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆