c++ - 使用 Boost.Random 从种子生成多精度整数

标签 c++ boost random mersenne-twister multiprecision

我正在尝试使用 C++ 的 Boost 多精度库来生成大的随机数。我一直无法创建一个按时间或另一个随机数播种的生成器,因此我的生成器在每次运行时都会生成相同的数字。我如何为生成器播种一个不断变化的值以在每次运行时产生不同的值?这是有效的代码,但每次运行都会产生相同的值:

 using namespace boost::multiprecision;
 using namespace boost::random;

 typedef independent_bits_engine<mt19937, 512, mpz_int> generator_type;
 generator_type gen;

 std::cout << gen() << "\n\n";

我之前成功播种了 std mersenne twister:

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 410);
std::cout << dis(gen);

但我不确定如何播种多精度 mt。如果我尝试将任何参数附加到 generator_type 声明,我会收到错误消息。

最佳答案

请确保以正确的顺序包含正确的 header 。确保 boost/multiprecision/random.hpp 包含在之前 例如boost/random.hpp[1]

这是一个工作示例:

Live On Coliru

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

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

    boost::mt19937 rng(3); // fixing seed for demo
    boost::uniform_int<Int> gen(-pow(Int(2), 400), pow(Int(2), 400));

    for (int i=0; i<10; ++i)
        std::cout << gen(rng) << "\n";
}

打印:

-1933652715378833784248363501979652496795524829874743132697181322648247480952527478485970668716806865063045317090084841622
-1468881213423638668843172777487968633185322950671475476288214928834762957270366851213470028408822700452414112732095789399
-438410269130756764874038685663894375462191729266787898021001121529040612201593866121171654735148672212107859934777249455
1640218057904403949431629353470797958943403389857715009204662011172706206212175540336638682612917363014646204359229208161
2080556950904231221688486212902649050443577133350992840521950777901511719409216800649680002332270542856870490906586634021
-2462847552934789794366130512379986584363897268428607239076390917679673552257507232435012961043902569359791960286013555735
1862125165202309929540318106374963238582997665808535945941185531430178511983671609033768595314282085775703389782906055681
-2252919975572088150601736662143078753669379374770846936106371833826830834376177961242332270710557376868189820866644291936
986998873018694187216384502983789929097242088320473495018118860428802373488463609060400540575932015408503979156759366945
111638721010166959954664901006097000984357549061159193446548907668369849648549091048841517202745565156043524728780018634

[1] 原理见标题:

namespace boost{ namespace random{ namespace detail{
//
// This is a horrible hack: this declaration has to appear before the definition of
// uniform_int_distribution, otherwise it won't be used...
// Need to find a better solution, like make Boost.Random safe to use with
// UDT's and depricate/remove this header altogether.
//
template<class Engine, class Backend, boost::multiprecision::expression_template_option ExpressionTemplates>
boost::multiprecision::number<Backend, ExpressionTemplates> 
   generate_uniform_int(Engine& eng, const boost::multiprecision::number<Backend, ExpressionTemplates>& min_value, const boost::multiprecision::number<Backend, ExpressionTemplates>& max_value);

}}}

关于c++ - 使用 Boost.Random 从种子生成多精度整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29705902/

相关文章:

c++ - 如何在 C++ 中将 static_assert 用于 constexpr 函数参数?

C++ 使用模板类指针作为模板参数而不给它一个参数

c++ - 在 protobuf 对象上调用析构函数

c++ - 可以在 Boost Graph Library 中即时计算边吗?

c++ - 使用boost库找到图形的最小切割

c++ - 使用自定义获取属性 boost 动态属性

matlab - matlab中的随机颜色矩阵

c++ - 符号 <> 是什么意思?

java - 产生随机输出 : 100 numbers from 901 to 999

java - 如何从 SplittableRandom 获取 nextFloat() ?