2015-06-26 53 views
1

this answer上,有一個使用boost::multiprecisionboost::random的最小示例。boost :: multiprecision中的隨機種子未能編譯

我與掙扎的例子,當我使用的種子:

#include <boost/multiprecision/random.hpp> 
#include <boost/random.hpp> 
#include <boost/multiprecision/cpp_int.hpp> 
#include <boost/multiprecision/cpp_dec_float.hpp> 
#include <boost/multiprecision/number.hpp> 

int main() { 
    namespace mp = boost::multiprecision; 

    boost::uniform_01<mp::cpp_dec_float_50> uf; 
    boost::random::independent_bits_engine<boost::mt19937, 50L * 1000L/301L, mp::number<mp::cpp_int::backend_type, mp::et_off> > gen; 

    gen.seed(1); // commenting this line compiles 

    std::cout << std::setprecision(50); 
    for (unsigned i = 0; i < 10; ++i) { 
     std::cout << uf(gen) << std::endl; 
    } 
    return 0; 
} 

其失敗,出現錯誤

/boost/random/detail/seed_impl.hpp:271:9: No member named 'generate' in 
'boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend< 
0, 0, 1, 0, std::__1::allocator<unsigned long long> >, 0>' 

Live On Coliru

有誰知道編譯爲什麼是這種情況,我怎樣才能爲種族創造種子R'

回答

1

您可以(?應該)使用seed_seq初始化發電機狀態這裏:

boost::random::seed_seq ss = { 12064, 3867, 13555, 28676, 4599, 5031, 13040 }; 
gen.seed(ss); 

看到它Live On Coliru

#include <boost/multiprecision/random.hpp> 
#include <boost/random.hpp> 
#include <boost/multiprecision/cpp_int.hpp> 
#include <boost/multiprecision/cpp_dec_float.hpp> 
#include <boost/multiprecision/number.hpp> 

int main() { 
    namespace mp = boost::multiprecision; 

    boost::uniform_01<mp::cpp_dec_float_50> uf; 
    boost::random::independent_bits_engine<boost::mt19937, 50L * 1000L/301L, mp::number<mp::cpp_int::backend_type, mp::et_off> > gen; 

    boost::random::seed_seq ss = { 12064, 3867, 13555, 28676, 4599, 5031, 13040 }; 
    gen.seed(ss); 

    std::cout << std::setprecision(50); 
    for (unsigned i = 0; i < 10; ++i) { 
     std::cout << uf(gen) << std::endl; 
    } 
} 

輸出:

0.1294215037989610513562087922172293748382444894925 
0.0011263048255423035737708210143957206443397839784902 
0.57090882872487031806846911445202567722713629651669 
0.9184596882556591787732169405089171594367204417453 
0.72134960897766980337512136740707080800748454242196 
0.22886603056354248070615658676991748981868589857292 
0.97729277591969460346190581363261507569948518236157 
0.66308744871655654022020255851215609000642867402575 
0.25024791464156020850213379847612778669700761888293 
0.86737050449459586351548567885641377917223025341979 
+1

序列可以只有一個條目,如'boost :: random :: seed_seq ss = {12 064};'。 [這個答案](http://stackoverflow.com/a/27867521/931303)解釋了爲什麼這是很好的情況。 –