1

On this answer, there is a minimal example of using boost::multiprecision with boost::random.

I'm struggling with that example when I use a seed:

#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::random::independent_bits_engine<boost::mt19937, 50L * 1000L / 301L, mp::number<mp::cpp_int::backend_type, mp::et_off> > gen;

    gen.seed(1);  // commenting this line compiles

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

which fails to compile with the error

/boost/random/detail/seed_impl.hpp:271:9: No member named 'generate' in 
'boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<
0, 0, 1, 0, std::__1::allocator<unsigned long long> >, 0>'

Live On Coliru

Does anyone knows why this is the case and how can I set a seed to the generator?

Community
  • 1
  • 1
Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121

1 Answers1

1

You can (should?) use a seed_seq to initialize the generator state here:

boost::random::seed_seq ss = { 12064, 3867, 13555, 28676, 4599, 5031, 13040 };
gen.seed(ss);

See it 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::random::independent_bits_engine<boost::mt19937, 50L * 1000L / 301L, mp::number<mp::cpp_int::backend_type, mp::et_off> > gen;

    boost::random::seed_seq ss = { 12064, 3867, 13555, 28676, 4599, 5031, 13040 };
    gen.seed(ss);

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

Output:

0.1294215037989610513562087922172293748382444894925
0.0011263048255423035737708210143957206443397839784902
0.57090882872487031806846911445202567722713629651669
0.9184596882556591787732169405089171594367204417453
0.72134960897766980337512136740707080800748454242196
0.22886603056354248070615658676991748981868589857292
0.97729277591969460346190581363261507569948518236157
0.66308744871655654022020255851215609000642867402575
0.25024791464156020850213379847612778669700761888293
0.86737050449459586351548567885641377917223025341979
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    The sequence can have just one entry, as in `boost::random::seed_seq ss = { 12064 };`. [This answer](http://stackoverflow.com/a/27867521/931303) explains why this is the case nicely. – Jorge Leitao Jun 26 '15 at 21:20