1 ///////////////////////////////////////////////////////////////
2 // Copyright 2011 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
6 #include <boost/multiprecision/gmp.hpp>
7 #include <boost/multiprecision/random.hpp>
14 //=#include <boost/multiprecision/gmp.hpp>
15 //=#include <boost/multiprecision/random.hpp>
19 using namespace boost::multiprecision;
20 using namespace boost::random;
23 // Declare our random number generator type, the underlying generator
24 // is the Mersenne twister mt19937 engine, and 256 bits are generated:
26 typedef independent_bits_engine<mt19937, 256, mpz_int> generator_type;
29 // Generate some values:
31 std::cout << std::hex << std::showbase;
32 for(unsigned i = 0; i < 10; ++i)
33 std::cout << gen() << std::endl;
42 //=#include <boost/multiprecision/gmp.hpp>
43 //=#include <boost/multiprecision/random.hpp>
47 using namespace boost::multiprecision;
48 using namespace boost::random;
51 // Generate integers in a given range using uniform_int,
52 // the underlying generator is invoked multiple times
53 // to generate enough bits:
56 uniform_int_distribution<mpz_int> ui(0, mpz_int(1) << 256);
58 // Generate the numbers:
60 std::cout << std::hex << std::showbase;
61 for(unsigned i = 0; i < 10; ++i)
62 std::cout << ui(mt) << std::endl;
72 //=#include <boost/multiprecision/gmp.hpp>
73 //=#include <boost/multiprecision/random.hpp>
77 using namespace boost::multiprecision;
78 using namespace boost::random;
80 // We need an underlying generator with at least as many bits as the
81 // floating point type to generate numbers in [0, 1) with all the bits
82 // in the floating point type randomly filled:
84 uniform_01<mpf_float_50> uf;
85 independent_bits_engine<mt19937, 50L*1000L/301L, mpz_int> gen;
87 // Generate the values:
89 std::cout << std::setprecision(50);
90 for(unsigned i = 0; i < 20; ++i)
91 std::cout << uf(gen) << std::endl;
100 //=#include <boost/multiprecision/gmp.hpp>
101 //=#include <boost/multiprecision/random.hpp>
105 using namespace boost::multiprecision;
106 using namespace boost::random;
108 // We can repeat the above example, with other distributions:
110 uniform_real_distribution<mpf_float_50> ur(-20, 20);
111 gamma_distribution<mpf_float_50> gd(20);
112 independent_bits_engine<mt19937, 50L*1000L/301L, mpz_int> gen;
114 // Generate some values:
116 std::cout << std::setprecision(50);
117 for(unsigned i = 0; i < 20; ++i)
118 std::cout << ur(gen) << std::endl;
119 for(unsigned i = 0; i < 20; ++i)
120 std::cout << gd(gen) << std::endl;