其他分享
首页 > 其他分享> > c-如何使用升压多精度生成普通随机数?

c-如何使用升压多精度生成普通随机数?

作者:互联网

我正在尝试使用boost的多精度从正态分布生成随机数.我可以从均匀分布中生成随机数,但是当我尝试从标准正态生成时,它会报告错误.
这是代码:

(从Examples from boost开始,将mpz_int更改为cpp_int,将mpf_float_50更改为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;
}

但是,我可以从均匀分布生成随机数,但是当我尝试从标准正态生成时,它显示:

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 / boost_1_58_0 / boost / random / detail / uniform_int_float.hpp:63:错误:类型无效的static_cast
‘boost :: multiprecision :: detail :: expression&gt ;,(boost :: multiprecision :: expression_template_option)1u&gt ;, boost :: multiprecision :: number&gt ;,(boost :: multiprecision :: expression_template_option)1u&gt ;, void, void>”键入“ long unsigned int”

我使用gcc版本4.4.7和64位linux系统.非常感谢.

(我也尝试使用boost的示例来生成随机数,但是仅通过include,我就会遇到很多错误,例如:
错误:“ struct boost :: multiprecision :: backends :: gmp_int”没有名为“ data”的成员)

我更改了一点代码:

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

这是完整的错误消息:

****用于项目test_boost的配置调试的增量构建****
全部制作
构建文件:../ test_boost.cpp
调用:Cross G编译器
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/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 / boost_1_58_0 / boost / random / detail / uniform_int_float.hpp:在成员函数中

‘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:从此处实例化

〜/ boost / boost_1_58_0 / boost / random / detail / uniform_int_float.hpp:63:错误:类型’boost :: multiprecision :: number< boost :: multiprecision :: backends :: cpp_int_backend< 0u,0u,(boost: :: umultiprecision :: cpp_integer_type)1u,(boost :: multiprecision :: cpp_int_check_type)0u,std :: allocator< unsigned int> &gt ;,(boost :: multiprecision :: expression_template_option)0u>”键入“ long unsigned int”

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

17:21:45构建完成(耗时910ms)

解决方法:

一些棘手的问题:

>包含顺序很重要(Using Boost.Random to generate multiprecision integers from a seed)
>,并且应将cpp_int参数的表达式模板禁用为Independent_bits.

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::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

标签:c,random,boost,normal-distribution
来源: https://codeday.me/bug/20191013/1908471.html