Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / multiprecision / example / random_snips.cpp
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_
5
6 #include <boost/multiprecision/gmp.hpp>
7 #include <boost/multiprecision/random.hpp>
8 #include <iostream>
9 #include <iomanip>
10
11 void t1()
12 {
13 //[random_eg1
14 //=#include <boost/multiprecision/gmp.hpp>
15 //=#include <boost/multiprecision/random.hpp>
16 //=
17 //=int main()
18 //={
19    using namespace boost::multiprecision;
20    using namespace boost::random;
21
22    //
23    // Declare our random number generator type, the underlying generator
24    // is the Mersenne twister mt19937 engine, and 256 bits are generated:
25    //
26    typedef independent_bits_engine<mt19937, 256, mpz_int> generator_type;
27    generator_type gen;
28    //
29    // Generate some values:
30    //
31    std::cout << std::hex << std::showbase;
32    for(unsigned i = 0; i < 10; ++i)
33       std::cout << gen() << std::endl;
34 //=   return 0;
35 //=}
36 //]
37 }
38
39 void t2()
40 {
41 //[random_eg2
42 //=#include <boost/multiprecision/gmp.hpp>
43 //=#include <boost/multiprecision/random.hpp>
44 //=
45 //=int main()
46 //={
47    using namespace boost::multiprecision;
48    using namespace boost::random;
49
50    //
51    // Generate integers in a given range using uniform_int,
52    // the underlying generator is invoked multiple times
53    // to generate enough bits:
54    //
55    mt19937 mt;
56    uniform_int_distribution<mpz_int> ui(0, mpz_int(1) << 256);
57    //
58    // Generate the numbers:
59    //
60    std::cout << std::hex << std::showbase;
61    for(unsigned i = 0; i < 10; ++i)
62       std::cout << ui(mt) << std::endl;
63
64 //=   return 0;
65 //=}
66 //]
67 }
68
69 void t3()
70 {
71 //[random_eg3
72 //=#include <boost/multiprecision/gmp.hpp>
73 //=#include <boost/multiprecision/random.hpp>
74 //=
75 //=int main()
76 //={
77    using namespace boost::multiprecision;
78    using namespace boost::random;
79    //
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:
83    //
84    uniform_01<mpf_float_50> uf;
85    independent_bits_engine<mt19937, 50L*1000L/301L, mpz_int> gen;
86    //
87    // Generate the values:
88    //
89    std::cout << std::setprecision(50);
90    for(unsigned i = 0; i < 20; ++i)
91       std::cout << uf(gen) << std::endl;
92 //=   return 0;
93 //=}
94 //]
95 }
96
97 void t4()
98 {
99 //[random_eg4
100 //=#include <boost/multiprecision/gmp.hpp>
101 //=#include <boost/multiprecision/random.hpp>
102 //=
103 //=int main()
104 //={
105    using namespace boost::multiprecision;
106    using namespace boost::random;
107    //
108    // We can repeat the above example, with other distributions:
109    //
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;
113    //
114    // Generate some values:
115    //
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;
121 //=   return 0;
122 //=}
123 //]
124 }
125
126 int main()
127 {
128    t1();
129    t2();
130    t3();
131    t4();
132    return 0;
133 }
134