如何使用 boost 多精度生成正态随机数?

How to generate normal random numbers using boost multiprecision?

我正在尝试使用 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 >, (boost::multiprecision::expression_template_option)1u>, boost::multiprecision::number >, (boost::multiprecision::expression_template_option)1u >, 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 调用:交叉 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: 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’

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

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

一些棘手的问题:

  • 包含顺序很重要 ()
  • 并且您应该禁用 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