2012-01-30 44 views
-1

我一直在玩Boost.Random一天,而boost::uniform_int_distribution<>效果很好,我遇到了問題boost::exponential_distribution<>如何使boost :: exponential_distribution工作?

一個簡單的程序是勝過千言萬語:

#include <iostream> 
#include <boost/random/mersenne_twister.hpp> 
#include <boost/random/exponential_distribution.hpp> 

int main() { 
    boost::mt19937 gen; 
    boost::exponential_distribution<> dis; 
    std::cout << dis(gen) << "\n"; 
    return 0; 
} 

編譯鏗鏘3.0,使用Boost 1.39.1(不,我不能升級加速)。

輸出總是相同的:nan

我找不到任何報告的問題,所以我想這是我沒有正確使用庫...任何線索將不勝感激。

+0

該代碼給了我最新的boost(1.48)與gcc 1.68591'(抱歉,不能幫助其他任何事情)。 – 2012-01-30 09:01:41

+0

@Jesse:感謝您的測試:) – 2012-01-30 09:16:09

回答

1

給定隨機數r,均勻分佈在[0,1)上,-log(r)分佈在(0,無窮遠),分佈爲exp(-x)。 前者是boost::uniform_01()

如果你需要一個分配p exp(-px),那麼它的-(1/p)log(r)

在這兩種情況下,這裏的log(x)都是自然對數(基數爲e)。

UPD:使用boost::variate_generator似乎爲我工作(提高1.43):

#include <iostream> 
#include<boost/random.hpp> 

int main() { 
    boost::mt19937 rng(11u); 
    boost::variate_generator< boost::mt19937&, boost::exponential_distribution<> > rndm(rng, boost::exponential_distribution<>()) ; 
    std::cout<<rndm()<<"\n"; 
} 

我沒有,雖然檢查的分佈。

+0

我確認通過'variate_generator'工作,感謝提示。現在,只要我明白有什麼區別... – 2012-01-30 09:16:49

+0

我不知道爲什麼它是這樣的:-(我個人會使用我的答案進行轉換並完成它。順便說一句,你一定知道在你的代碼片段你沒有播種發電機? – 2012-01-30 10:28:56

+0

我現在使用你的技巧,我正在查看'variate_generator'源代碼。至於不播種,這是爲了讓我可以在每次運行時產生相同的輸出,同時進行微調我的算法,一旦我完成,我會使用一些基於時間的種子或其他東西。 – 2012-01-30 12:57:20

相關問題