OS X libc++ std::uniform_real_distribution bug

OS X libc++ std::uniform_real_distribution bug

本文关键字:real distribution bug uniform std OS libc++      更新时间:2023-10-16

在使用Apple LLVM 7.0.2版(clang-700.1.81)编译C++11的std::uniform_real_distribution时,我看到了一些奇怪的行为。调用operator()会导致结果超出分发范围。下面的最小样本程序再现了故障

// Example program
#include <random>
#include <iostream>
#include <string>
template< int power >
constexpr uint64_t power_of_two(){
return 2 * power_of_two< power - 1 >();
}
template< >
constexpr uint64_t power_of_two< 0 >(){
return 1;
}
std::linear_congruential_engine
< uint64_t, 273673163155, 13, power_of_two< 48 >() >
rng;
std::uniform_real_distribution< double > angle_cosine(-1, 1);
int main()
{
std::cout << angle_cosine.a() << " " << angle_cosine.b() << 'n' << std::endl;
for (int i = 0; i < 4; ++i){
std::cout << angle_cosine(rng) << std::endl;
}
}

在线编译和运行(可能使用g++)会产生合理的结果

-1 1
-0.529254
-0.599452
0.513316
-0.604338

然而,在本地编译和运行会产生不合理的结果。

-1 1
130349
37439.4
42270.5
45335.4

我是否忽略了什么,或者在libc++中遇到了错误?如果是后者,有人知道变通办法吗?

这是LLVM线性同余生成器中的一个错误,而不是均匀实分布中的一。均匀实数分布假设生成器返回的数字在生成器的最小值和最大值之间(包括最小值和最小值)。这是任何发电机的要求。具有这组数的LLVM线性同余生成器不能满足这一要求。

LLVM线性同余生成器使用一种称为Schrage算法的旧算法来避免溢出,而不是数字集的(a*x+c)%m。该算法的LLVM实现实际上保证了a, c, m集合将生成大于m的数字。

您可以在此处查看LLVM代码:https://llvm.org/svn/llvm-project/libcxx/trunk/include/random。搜索"Schrage算法"。选择acm会调用该术语四次出现中的第一次。

顺便说一句,你的代码中也有一个bug。那些神奇的数字273673163155和13应该是以8为底。这些是drand48使用的数字。随机选取acm的值几乎肯定会导致坏的随机数生成器。

我建议切换到std::mt19937std::mt19937_64