gpt4 book ai didi

c++ - 随机在 boost::multiprecision 中,种子无法编译

转载 作者:行者123 更新时间:2023-11-30 05:41:41 28 4
gpt4 key购买 nike

关于 this answer ,有一个使用 boost::multiprecisionboost::random 的最小示例。

当我使用种子时,我正在努力解决这个例子:

#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

有谁知道为什么会这样,我该如何为生成器设置种子?

最佳答案

您可以(应该?)使用 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

关于c++ - 随机在 boost::multiprecision 中,种子无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31066882/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com