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)
A few tricky bits:
cpp_int
parameter toindependent_bits
.Live On Coliru
Prints e.g.