How to generate normal random numbers using boost

2020-04-21 07:51发布

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:

(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/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: 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’

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

(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’)

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:

**** Incremental Build of configuration Debug for project test_boost **** make all Building file: ../test_boost.cpp Invoking: Cross G++ Compiler 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"

In file included from

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

from

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

from

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

from

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

from

../test_boost.cpp:114:

~/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: instantiated from here

~/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] Error 1

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

1条回答
劳资没心,怎么记你
2楼-- · 2020-04-21 08:37

A few tricky 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;
}

Prints e.g.

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
查看更多
登录 后发表回答