使用Boost.Random生成从种子多precision整数 [英] Using Boost.Random to generate multiprecision integers from a seed

查看:283
本文介绍了使用Boost.Random生成从种子多precision整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Boost多precision库C ++生成大量随机数。我一直无法创造这是由时间或其他随机数种子的发电机,所以我的发生器产生在每次运行相同的号码。我如何籽变化的值发电机在每次运行时产生不同的价值观?这里是code这工作,但产生在每次运行相同的值:

 使用空间boost ::多precision;
 使用空间boost ::随机的; typedef的independent_bits_engine< mt19937,512,mpz_int> generator_type;
 generator_type根; 性病::法院LT&;<根()<< \\ n \\ n;

我已经成功播种前性病梅森倍捻机:

 的std :: random_device次;
的std :: mt19937根(RD());
的std :: uniform_int_distribution<> DIS(1,410);
性病::法院LT&;< DIS(根);

但我不知道如何种子多precision吨。我得到一个错误,如果我尝试任何参数附加到generator_type声明。


解决方案

只是一定要包括以正确的顺序正确的头。确保包括升压/多precision / random.hpp 之前例如升压/ random.hpp [1]

下面是一个工作示例:

<大骨节病> 住在Coliru

 的#include&LT;升压/多precision / gmp.hpp&GT;
#包括LT&;升压/多precision / random.hpp&GT;
#包括LT&;&iostream的GT;诠释的main()
{
    命名空间MP =提振::多precision;
    使用INT = MP :: mpz_int;    提高:: mt19937 RNG(3); //用于演示固定种子
    提高:: uniform_int&LT;&诠释GT;根(-pow(中间体(2),400),POW(智力(2),400));    对(INT I = 0; I&小于10 ++ⅰ)
        性病::法院LT&;&LT;根(RNG)&LT;&LT; \\ n;
}

打印:

<$p$p><$c$c>-1933652715378833784248363501979652496795524829874743132697181322648247480952527478485970668716806865063045317090084841622
-1468881213423638668843172777487968633185322950671475476288214928834762957270366851213470028408822700452414112732095789399
-438410269130756764874038685663894375462191729266787898021001121529040612201593866121171654735148672212107859934777249455
1640218057904403949431629353470797958943403389857715009204662011172706206212175540336638682612917363014646204359229208161
2080556950904231221688486212902649050443577133350992840521950777901511719409216800649680002332270542856870490906586634021
-2462847552934789794366130512379986584363897268428607239076390917679673552257507232435012961043902569359791960286013555735
1862125165202309929540318106374963238582997665808535945941185531430178511983671609033768595314282085775703389782906055681
-2252919975572088150601736662143078753669379374770846936106371833826830834376177961242332270710557376868189820866644291936
986998873018694187216384502983789929097242088320473495018118860428802373488463609060400540575932015408503979156759366945
111638721010166959954664901006097000984357549061159193446548907668369849648549091048841517202745565156043524728780018634


[1] 有关理由查看标题:

 空间boost {随机命名空间{{详细
//
//这是一个可怕的黑客:这个声明有定义之前出现
// uniform_int_distribution,否则将不被使用...
//需要找到一个更好的解决方案,如化妆Boost.Random安全与使用
// UDT的和贬值了/完全消除这个头。
//
模板&LT;类引擎类后端,提振::多precision ::前pression_template_option前pressionTemplates&GT;
提高::多precision ::号&LT;后端,防爆pressionTemplates&GT;
   generate_uniform_int(发动机和安培;工程,常量的boost ::多precision ::号&LT;后端,防爆pressionTemplates&GT;&安培; MIN_VALUE,常量的boost ::多precision ::号&LT;后端,防爆pressionTemplates&GT;&安培; MAX_VALUE);}}}

I am attempting to use the Boost multiprecision libraries for C++ to generate large random numbers. I have been unable to create a generator which is seeded by the time or another random number, so my generator produces the same numbers on every run. How do I seed the generator with a changing value to produce different values on each run? Here is the code which works but produces the same values on each run:

 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";

I have seeded the std mersenne twister before with success:

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

but I am not sure how to seed the multiprecision mt. I get an error if I try to attach any arguments to the generator_type declaration.

解决方案

Just be sure to include the right headers in the right order. Make sure boost/multiprecision/random.hpp is included before e.g. boost/random.hpp. [1]

Here's a working sample:

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";
}

Prints:

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


[1] For rationale see the header:

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);

}}}

这篇关于使用Boost.Random生成从种子多precision整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆