std :: piecewise_linear_distribution不能在VS2012下运行?

时间:2013-04-14 14:47:06

标签: c++ visual-c++ random visual-studio-2012 c++11

我决定更好地了解c ++ 11 <random>,所以我编写了这样的代码:

std::mt19937 gen(10);
std::piecewise_linear_distribution<> d(Range.begin(), Range.end(),
                                       RangeValues.begin());

std::map<int, unsigned int> hist;
for (int n = 0; ++n != iterations;)
    ++hist[std::round(d(gen))];

for (auto p : hist)
   std::cout << std::setw(2) << p.first << ": "
             << std::string(p.second/(iterations/200), '*') << '\n';

由于某些原因,std::random_device似乎对Coliru不起作用,所以我输入了一个const样本种子。我认为,它是UB,因此IIRC它主要依赖于硬件,这就是为什么Coliru不支持它(如果我错了,请纠正我)。但是在Ideone上,它确实有效。

然后我移植它在VS2012上运行,唯一的区别是std::round的自己实现:

return number < 0.0 ? std::ceil(number - 0.5) : std::floor(number + 0.5);

它在Coliru上完全正确,但是当我在VS2012上编译并运行它时the output is just wrong

任何想法如何纠正这一点,更重要的是,为什么会发生这种情况? 我做了一些迟钝的事情,还是VS2012不是聪明人?

1 个答案:

答案 0 :(得分:7)

这似乎是Visual Studio问题。我已经尝试了下面的程序(改编自OP),GCC 4.7.2,Clang 3.2和Intel 13.1.0生成的输出非常合理,而Visual Studio 2012年11月CTP生成的输出完全不同。

概率密度是分段线性的,并且由以下方式由数组x和p定义。建立连接点(x [i],p [i])的分段线性函数,其中i = 0,...,N(其中N = x.size() - 1)。然后将此函数归一化(通过除以其积分)以获得概率密度。

#include <iostream>
#include <iomanip>
#include <string>
#include <random>
#include <array>

int main() {

    std::mt19937 gen(10);

    std::array<double, 3> x = {{0, 20, 40}};
    std::array<double, 3> p = {{0,  1,  0}};
    std::piecewise_linear_distribution<> dist(x.begin(), x.end(), p.begin());

    std::array<int, 40> hist = {{0}};

    for (size_t i = 0; i < 200000; ++i)
        ++hist[static_cast<size_t>(dist(gen))];

    for (size_t n = 0; n < hist.size(); ++n)
        std::cout << std::setfill('0') << std::setw(2) << n << ' ' << 
          std::string(hist[n] / 200, '*') << std::endl;

    std::cout << "\nValues in interval [20, 21[ : " << hist[20] << std::endl;
}

在我们的示例中,多边形函数连接(0,0),(20,1)和(40,0)。因此,其形状是具有基部40和高度1的等腰三角形,其产生20的面积。因此,概率密度f连接(0,0),(20,1 / 20)和(40,0)。这意味着在区间[20,21 [我们可以预期f(20)*(21 - 20)= 1/20 * 1 = 1/20的结果。总共我们绘制了200,000个值,然后,我们可以预期在[20,21 [。

海湾合作委员会,Clang和英特尔在[20,21]中报告了9734分[并显示了与等腰三角形非常相似的模式:

00 *
01 ***
02 *****
03 ********
04 ***********
05 **************
06 ***************
07 ******************
08 ********************
09 ************************
10 **************************
11 ****************************
12 *******************************
13 *********************************
14 ***********************************
15 ***************************************
16 *****************************************
17 ******************************************
18 **********************************************
19 ************************************************
20 ************************************************
21 *********************************************
22 *******************************************
23 *****************************************
24 **************************************
25 ************************************
26 **********************************
27 ******************************
28 ****************************
29 **************************
30 ***********************
31 ********************
32 ******************
33 ****************
34 *************
35 ***********
36 *********
37 ******
38 ***
39 *

Values in interval [20, 21[ : 9734

不幸的是,Visual Studio 2012年11月的CTP给出了这个:

00 ********************************************** [truncated]
01 **********************************************
02 ***********************************
03 *****************************
04 **************************
05 ***********************
06 *********************
07 ********************
08 *******************
09 ******************
10 *****************
11 ****************
12 ***************
13 **************
14 **************
15 **************
16 *************
17 *************
18 *************
19 ************
20 ************
21 *************
22 *************
23 *************
24 *************
25 **************
26 ***************
27 ***************
28 ****************
29 *****************
30 ******************
31 *******************
32 *******************
33 *********************
34 ***********************
35 **************************
36 *****************************
37 ***********************************
38 **********************************************
39 ********************************************** [truncated]

Values in interval [20, 21[ : 2496

注意:

  1. 我已截断Visual Studio输出以便更好地显示。
  2. 对[20,21]中的点数的更好估计[是200,000 *(0.5 *(f(20)+ f(21)))*(21 - 20)= 100,000 *(1/20 + 1 / 20 - 1/400)= 10,000 - 250 = 9750.