2016-04-21 32 views
0

當使用clang++實現std::binomial_distribution,我看到了一些非常不滿的行爲:C++ 11的std :: binomial_distribution不穩定

default_random_engine rng; 

vector<double> p{0.2500000000000000000000000000000000000000, 
       0.3333333333333333148296162562473909929395, 
       0.5999999999999999777955395074968691915274, 
       1.0000000000000002220446049250313080847263}; 

for (auto &i : p) { 
    binomial_distribution<int> binom(1000, i); 
    cout << binom(rng) << " ≈ " << 1000 * i << endl; 
} 

下面是結果:

254 ≈ 250 
340 ≈ 333.333 
598 ≈ 600 
628 ≈ 1000 # WAT? - should be around 1000 

很奇怪的是,如果你嘗試撥打binomial_distribution<int>(100, p[3]),它永遠掛起。這後一種行爲後來是不受歡迎的,但預計 - 成功的可能性不應該超過1

如果您想知道,p的上述值來自使用conditional binomial method修改gsl_ran_multinomial來生成多項式變量。

我的編譯器是clang++El Capitan 10.11.4

Apple LLVM version 7.3.0 (clang-703.0.29) 
Target: x86_64-apple-darwin15.4.0 

謝謝!

回答

1

正如您已經指出的那樣,p(成功的概率)必須小於或等於1.看起來,傳遞一個無效的值(與Clang中的當前實現一樣)使得它重新使用以前的值(你的情況爲0.5999)。

總之,你似乎在調用未定義的行爲。什麼事情都可能發生。當p超出範圍時,Boost的實現是documented,因爲拋出異常。

+0

有道理。我想我真的很想抱怨。 – Manbroski

相關問題