本文介绍了如何使用Boost多precision生成正常的随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用产生提振的多precision正态分布随机数。我可以从均匀分布生成随机数,但是当我试图从标准的正常产生,它会报告错误。
这里是code:

I'm trying to generate random numbers from normal distribution using boost's multiprecision. I can generate random numbers from uniform distributions, but when I tried to generate from standard normal, it reports an error. Here is the code:

(从<一个如下href=\"http://www.boost.org/doc/libs/1_58_0/libs/multi$p$pcision/doc/html/boost_multi$p$pcision/tut/random.html\"相对=nofollow>从boost ,改变mpz_int到cpp_int,并mpf_float_50到cpp_dec_float_50)示例

(follows from Examples from boost, change mpz_int to cpp_int, and mpf_float_50 to cpp_dec_float_50)

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

int main()
{
   using namespace boost::multiprecision;
   using namespace boost::random;

   uniform_01<cpp_dec_float_50> uf;
   normal_distribution<cpp_dec_float_50> n01(0.0, 1.0);
   independent_bits_engine<mt19937, 50L*1000L/301L, cpp_int> gen;

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

不过,我可以从均匀分布生成随机数,但是当我试图从标准的正常产生,这说明:

However, I can generate random numbers from uniform distributions, but when I tried to generate from standard normal, it shows:

G ++ -I /〜/升压/ boost_1_58_0 -O0 -g3 -Wall -c -fmessage长度= 0 -MMD -MP -MFtest_boost.d-MTtest_boost.d-otest_boost.o ../ test_boost.cpp

g++ -I/~/boost/boost_1_58_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"test_boost.d" -MT"test_boost.d" -o "test_boost.o" "../test_boost.cpp"

升压/ boost_1_58_0 /升压/随机/细节/ uniform_int_float.hpp:63:错误:无效的类型的static_cast
提振::多precision ::详细::前pression>,(提高::多precision ::前pression_template_option)1U>,提振::多precision: :号码>,(提高::多precision ::前pression_template_option)1U>,虚空,虚空>输入长期无符号整型

boost/boost_1_58_0/boost/random/detail/uniform_int_float.hpp:63: error: invalid static_cast from type ‘boost::multiprecision::detail::expression >, (boost::multiprecision::expression_template_option)1u>, boost::multiprecision::number >, (boost::multiprecision::expression_template_option)1u>, void, void>’ to type ‘long unsigned int’

我用gcc版本4.4.7,和64位Linux系统。谢谢你这么多。

I use gcc version 4.4.7, and 64 bit linux system. Thank you so much.

(我还试图用升压转换器的例子来产生随机数,但我有很多的错误只需通过包括,如:
错误:'结构的boost ::多precision ::后端:: gmp_int没有名为数据

(I also tried to use boost's example to generate random numbers, but I have a lot of errors simply by include , such as:error: ‘struct boost::multiprecision::backends::gmp_int’ has no member named ‘data’)

我改变了code一点点:

I changed the code a little bit:

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

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

    boost::uniform_01<mp::cpp_dec_float_50> uf;
    boost::normal_distribution<mp::cpp_dec_float_50> n01(0.0, 1.0);

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

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

下面是完整的错误消息:

Here is the full error message:

****项目test_boost配置调试的增量构建****
让所有
构建文件:../test_boost.cpp
调用:跨G ++编译器
G ++ -I /〜/升压/ boost_1_58_0 -O0 -g3 -Wall -c -fmessage长度= 0 -MMD -MP -MFtest_boost.d-MTtest_boost.d-otest_boost.o。 ./test_boost.cpp

**** Incremental Build of configuration Debug for project test_boost ****make all Building file: ../test_boost.cppInvoking: Cross G++ Compilerg++ -I/~/boost/boost_1_58_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"test_boost.d" -MT"test_boost.d" -o "test_boost.o" "../test_boost.cpp"

在自包含的文件

~/boost/boost_1_58_0/boost/random/uniform_int_distribution.hpp:29,

~/boost/boost_1_58_0/boost/random/random_number_generator.hpp:20,

~/boost/boost_1_58_0/boost/random.hpp:54,

~/boost/boost_1_58_0/boost/multiprecision/random.hpp:31,

../test_boost.cpp:114:

〜/升压/ boost_1_58_0 /升压/随机/细节/ uniform_int_float.hpp:在成员函数

~/boost/boost_1_58_0/boost/random/detail/uniform_int_float.hpp: In member function

‘typename boost::uint_t<((std::numeric_limits::digits < std::numeric_limits::digits) ? std::numeric_limits::digits :  std::numeric_limits::digits)>::fast boost::random::detail::uniform_int_float<URNG>::operator()() [with URNG = boost::random::independent_bits_engine<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, 166ul, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1u, (boost::multiprecision::cpp_int_check_type)0u, std::allocator<unsigned int> >, (boost::multiprecision::expression_template_option)0u> >]’:

~/boost/boost_1_58_0/boost/random/uniform_int_distribution.hpp:67:   instantiated from ‘T boost::random::detail::generate_uniform_int(Engine&, T, T, mpl_::true_) [with Engine = boost::random::detail::uniform_int_float<boost::random::independent_bits_engine<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, 166ul, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1u, (boost::multiprecision::cpp_int_check_type)0u, std::allocator<unsigned int> >, (boost::multiprecision::expression_template_option)0u> > >, T = int]’

~/boost/boost_1_58_0/boost/random/uniform_int_distribution.hpp:222:   instantiated from ‘T boost::random::detail::generate_uniform_int(Engine&, T, T, mpl_::false_) [with Engine = boost::random::independent_bits_engine<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, 166ul, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1u, (boost::multiprecision::cpp_int_check_type)0u, std::allocator<unsigned int> >, (boost::multiprecision::expression_template_option)0u> >, T = int]’

~/boost/boost_1_58_0/boost/random/uniform_int_distribution.hpp:230:   instantiated from ‘T boost::random::detail::generate_uniform_int(Engine&, T, T) [with Engine = boost::random::independent_bits_engine<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, 166ul, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1u, (boost::multiprecision::cpp_int_check_type)0u, std::allocator<unsigned int> >, (boost::multiprecision::expression_template_option)0u> >, T = int]’

~/boost/boost_1_58_0/boost/random/uniform_int_distribution.hpp:353:   instantiated from ‘IntType boost::random::uniform_int_distribution<IntType>::operator()(Engine&) const [with Engine = boost::random::independent_bits_engine<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, 166ul, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1u, (boost::multiprecision::cpp_int_check_type)0u, std::allocator<unsigned int> >, (boost::multiprecision::expression_template_option)0u> >, IntType = int]’

~/boost/boost_1_58_0/boost/random/normal_distribution.hpp:195:   instantiated from ‘std::pair<RealType, int> boost::random::detail::generate_int_float_pair(Engine&, mpl_::false_) [with RealType = boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<50u, int, void>, (boost::multiprecision::expression_template_option)1u>, long unsigned int w = 8ul, Engine = boost::random::independent_bits_engine<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, 166ul, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1u, (boost::multiprecision::cpp_int_check_type)0u, std::allocator<unsigned int> >, (boost::multiprecision::expression_template_option)0u> >]’

~/boost/boost_1_58_0/boost/random/normal_distribution.hpp:205:   instantiated from ‘std::pair<RealType, int> boost::random::detail::generate_int_float_pair(Engine&) [with RealType = boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<50u, int, void>, (boost::multiprecision::expression_template_option)1u>, long unsigned int w = 8ul, Engine = boost::random::independent_bits_engine<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, 166ul, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1u, (boost::multiprecision::cpp_int_check_type)0u, std::allocator<unsigned int> >, (boost::multiprecision::expression_template_option)0u> >]’

~/boost/boost_1_58_0/boost/random/normal_distribution.hpp:216:   instantiated from ‘RealType `boost::random::detail::unit_normal_distribution<RealType>::operator()(Engine&) [with Engine = boost::random::independent_bits_engine<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, 166ul, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1u, (boost::multiprecision::cpp_int_check_type)0u, std::allocator<unsigned int> >, (boost::multiprecision::expression_template_option)0u> >, RealType = boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<50u, int, void>, (boost::multiprecision::expression_template_option)1u>]’`

~/boost/boost_1_58_0/boost/random/normal_distribution.hpp:357:   instantiated from ‘RealType `boost::random::normal_distribution<RealType>::operator()(Engine&) [with Engine = boost::random::independent_bits_engine<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, 166ul, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1u, (boost::multiprecision::cpp_int_check_type)0u, std::allocator<unsigned int> >, (boost::multiprecision::expression_template_option)0u> >, RealType = boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<50u, int, void>, (boost::multiprecision::expression_template_option)1u>]’`

../ test_boost.cpp:135:从这里实例化

../test_boost.cpp:135: instantiated from here

〜/升压/ boost_1_58_0 /升压/随机/细节/ uniform_int_float.hpp:63:错误:从类型无效的static_cast '的boost ::多precision: :数字&LT;提高::多precision ::后端:: cpp_int_backend&LT; 0U,0U,(提高::多precision :: cpp_integer_type)1U,(提高::多precision :: cpp_int_check_type) 0U,性病::分配器&LT; unsigned int类型&GT; &gt;中(升压::多precision ::前pression_template_option)0U&GT;'输入'长unsigned int类型'

~/boost/boost_1_58_0/boost/random/detail/uniform_int_float.hpp:63: error: invalid static_cast from type ‘boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1u, (boost::multiprecision::cpp_int_check_type)0u, std::allocator<unsigned int> >, (boost::multiprecision::expression_template_option)0u>’ to type ‘long unsigned int’

请:*** [test_boost.o]错误1

make: *** [test_boost.o] Error 1

17时21分45秒构建成品(带910ms)

17:21:45 Build Finished (took 910ms)

推荐答案

一个棘手的几个位:


  • 纳入顺序很重要(<一个href=\"http://stackoverflow.com/questions/29705902/using-boost-random-to-generate-multi$p$pcision-integers-from-a-seed/29722989#29722989\">Using Boost.Random从种子生成多precision整数)

  • ,你应该禁用的 cpp_int 参数e pression模板 independent_bits

  • the inclusion order is important (Using Boost.Random to generate multiprecision integers from a seed)
  • and you should disable expression templates for the cpp_int parameter to independent_bits.

#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::normal_distribution<mp::cpp_dec_float_50> n01(0.0, 1.0);

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

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

例如打印

0.071158143395663314294038846164419405590199598107149
-3.386122969689401403435039837396914808444794978187
0.8764330891239492672300640710157085727853216046851
-2.0064145894745984574924806549098893735202520612338
0.35063440434471256645248013148941661436898526919012
-2.6716586312329566575912648384475719362060750451386
0.55126463254396465483421760539251997054451429475851
-1.7729411307129102331140853820760877874503063867511
0.23451591287440404629503274013000569210570783079923
-1.8436859957276497364219420143355960542394553824973
0.67246064287165530800489640577641451760040603708175
-2.7866182566345250589888851601603928202838710045557
0.80792574281283013736403300555330454345085259792047
-0.15055052607056848408105897310815019836250664353778
0.37045508031046743582526542286707476033397122878445
-0.89212086300647281063038787100486855752802424330061
0.057114478905607662739791206480165678105786999645263
-2.6490252463914095599465380526414962516123643879414
0.58651613738479171168557610089809027540801543233291
-0.44308599806450532102588370830806367936934083181489

这篇关于如何使用Boost多precision生成正常的随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 04:39