]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/example/random_snips.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / 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/cpp_int.hpp>
7 #include <boost/multiprecision/cpp_bin_float.hpp>
8 #include <boost/random.hpp>
9 #include <boost/scoped_ptr.hpp>
10 #include <iostream>
11 #include <iomanip>
12
13 void t1()
14 {
15 //[random_eg1
16 //=#include <boost/multiprecision/cpp_int.hpp>
17 //=#include <boost/random.hpp>
18 //=
19 //=int main()
20 //={
21 using namespace boost::multiprecision;
22 using namespace boost::random;
23
24 //
25 // Declare our random number generator type, the underlying generator
26 // is the Mersenne twister mt19937 engine, and we'll generate 256 bit
27 // random values, independent_bits_engine will make multiple calls
28 // to the underlying engine until we have the requested number of bits:
29 //
30 typedef independent_bits_engine<mt19937, 256, cpp_int> generator_type;
31 generator_type gen;
32 //
33 // Generate some values:
34 //
35 std::cout << std::hex << std::showbase;
36 for(unsigned i = 0; i < 10; ++i)
37 std::cout << gen() << std::endl;
38 //
39 // Alternatively if we wish to generate random values in a fixed-precision
40 // type, then we must use an unsigned type in order to adhere to the
41 // conceptual requirements of the generator:
42 //
43 typedef independent_bits_engine<mt19937, 512, uint512_t> generator512_type;
44 generator512_type gen512;
45 //
46 // Generate some 1024-bit unsigned values:
47 //
48 std::cout << std::hex << std::showbase;
49 for(unsigned i = 0; i < 10; ++i)
50 std::cout << gen512() << std::endl;
51 //= return 0;
52 //=}
53 //]
54 }
55
56 //
57 // Output from t1() is:
58 //[random_eg1_out
59 /*`[pre
60 0xD091BB5C22AE9EF6E7E1FAEED5C31F792082352CF807B7DFE9D300053895AFE1
61 0xA1E24BBA4EE4092B18F868638C16A625474BA8C43039CD1A8C006D5FFE2D7810
62 0xF51F2AE7FF1816E4F702EF59F7BADAFA285954A1B9D09511F878C4B3FB2A0137
63 0xF508E4AA1C1FE6527C419418CC50AA59CCDF2E5C4C0A1F3B2452A9DC01397D8D
64 0x6BF88C311CCA797AEA6DA4AEA3C78807CACE1969E0E0D4ADF5A14BAB80F00988
65 0xA7DE9F4CCC450CBA0924668F5C7DC380D96089C53640AC4CEF1A2E6DAE6D9426
66 0xADC1965B6613BA46C1FB41C2BD9B0ECDBE3DEDFC7989C8EE6468FD6E6C0DF032
67 0xA7CD66342C826D8B2BD2E4124D4A2DBEB4BF6FA7CC1A89590826328251097330
68 0x46E46CB0DF577EC20BD1E364262C556418DDA0C9FE7B45D9D2CE21C9D268409A
69 0xB1E049E1200BFA47512D6E73C3851EEEF341C0817D973E4808D17554A9E20D28
70 0xD091BB5C22AE9EF6E7E1FAEED5C31F792082352CF807B7DFE9D300053895AFE1A1E24BBA4EE4092B18F868638C16A625474BA8C43039CD1A8C006D5FFE2D7810
71 0xF51F2AE7FF1816E4F702EF59F7BADAFA285954A1B9D09511F878C4B3FB2A0137F508E4AA1C1FE6527C419418CC50AA59CCDF2E5C4C0A1F3B2452A9DC01397D8D
72 0x6BF88C311CCA797AEA6DA4AEA3C78807CACE1969E0E0D4ADF5A14BAB80F00988A7DE9F4CCC450CBA0924668F5C7DC380D96089C53640AC4CEF1A2E6DAE6D9426
73 0xADC1965B6613BA46C1FB41C2BD9B0ECDBE3DEDFC7989C8EE6468FD6E6C0DF032A7CD66342C826D8B2BD2E4124D4A2DBEB4BF6FA7CC1A89590826328251097330
74 0x46E46CB0DF577EC20BD1E364262C556418DDA0C9FE7B45D9D2CE21C9D268409AB1E049E1200BFA47512D6E73C3851EEEF341C0817D973E4808D17554A9E20D28
75 0x70518CE6203AC30361ADD0AB35D0430CC3F8E8920D1C8509CB92388E095436BF2FD6E20868A29AF97D61330B753EC6FC7211EFEA7CD15133A574C4FFCB41F198
76 0xB598EEF6EBBE7347C1332568CEBA5A7046A99459B4AD9F11AE00FEAA00B8B573A7B480B6B5F0B06C29A0EC27A4DAA0101E76A1C574BE91337F94C950C61F6ED6
77 0xF5B1C7A192E195F8572384D4E0732C8895D41B68CEE496C3394BBD52048CD47CC05309BED23D2D63414DE9C5D2229F23818666A3F0A8B109B2F6B12769A48341
78 0xE4123C566C548C8FF5941F6194B993AA8C1651342876763C237CE42EC300D11B263821CA3AEB820241EC0F84CF4AC36DD7393EE6FD0FC06A4118A30A551B54A4
79 0xD074F86F4CC1C54A3E57A70303774CDAEDE43895379CE62759988939E8490DDC325410E1D9352F6A4047080AF47C081D9DB51A85C765D71F79297527FCCA2773
80 ]
81 */
82 //]
83
84
85 void t2()
86 {
87 std::cout << std::dec;
88 //[random_eg2
89 //=#include <boost/multiprecision/cpp_int.hpp>
90 //=#include <boost/random.hpp>
91 //=
92 //=int main()
93 //={
94 using namespace boost::multiprecision;
95 using namespace boost::random;
96
97 //
98 // Generate integers in a given range using uniform_int,
99 // the underlying generator is invoked multiple times
100 // to generate enough bits:
101 //
102 mt19937 mt;
103 uniform_int_distribution<cpp_int> ui(-(cpp_int(1) << 256), cpp_int(1) << 256);
104 //
105 // Generate the numbers:
106 //
107 for(unsigned i = 0; i < 10; ++i)
108 std::cout << ui(mt) << std::endl;
109
110 //= return 0;
111 //=}
112 //]
113 }
114 //[random_eg2_out
115 /*`
116 Program output is
117
118 [pre
119 25593993629538149833210527544371584707508847463356155903670894544241785158492
120 12721121657520147247744796431842326146296294180809160027132416389225539366745
121 106034929479008809862776424170460808190085984129117168803272987114325199071833
122 86048861429530654936263414134573980939351899046345384016090167510299251354700
123 -23473382144925885755951447143660880642389842563343761080591177733698450031250
124 76840269649240973945508128641415259490679375154523618053296924666747244530145
125 21638369166612496703991271955994563624044383325105383029306009417224944272131
126 18829152205014764576551421737727569993966577957447887116062495161081023584880
127 101521572847669971701030312596819435590097618913255156117898217707115132658117
128 -97490271301923067621481012355971422109456300816856752380346627103308328292057
129 ]
130 */
131 //]
132
133 void t3()
134 {
135 //[random_eg3
136 //=#include <boost/multiprecision/cpp_bin_float.hpp>
137 //=#include <boost/random.hpp>
138 //=
139 //=int main()
140 //={
141 using namespace boost::multiprecision;
142 using namespace boost::random;
143
144 mt19937 gen;
145 //
146 // Generate the values:
147 //
148 std::cout << std::setprecision(50);
149 for(unsigned i = 0; i < 20; ++i)
150 std::cout << generate_canonical<cpp_bin_float_50, std::numeric_limits<cpp_bin_float_50>::digits>(gen) << std::endl;
151 //= return 0;
152 //=}
153 //]
154 }
155
156 //[random_eg3_out
157 /*`
158 Which produces the following output:
159
160 [pre
161 0.96886777112423135248554451482797431507115448261086
162 0.54722059636785192454525760726084778627750790023546
163 0.99646132554800874317788284808573062871409279729804
164 0.98110969177693891782396443737643892769773768718591
165 0.29702944955795083040856753579705872634075574515969
166 0.63976335709815275010379796044374742646738557798647
167 0.79792861516022605265555700991255998690336456180995
168 0.68135953856026596523755400091345037778580909233387
169 0.47475868061723477935404326837783394169122045199915
170 0.30191312687731969398296589840622989141067852863748
171 0.87242882006730022427155209451091472382531795659709
172 0.82190326480741096300318873712966555706035846579562
173 0.49058903962146072778707295967429263659897501512813
174 0.2102090745190061764133345429475530760261103345204
175 0.4087311609617603484960794513055502599728804206333
176 0.79397497154919267900450180642484943996546102712187
177 0.70577425166871982574205252142383800792823003687121
178 0.64396095652194035523385641523010248768636064728226
179 0.5737546665965914620678634509134819579811035412969
180 0.017773895576552474810236796736785695789752666554273
181 ]
182 */
183 //]
184
185 void t4()
186 {
187 std::cout << std::endl;
188 //[random_eg4
189 //=#include <boost/multiprecision/cpp_bin_float.hpp>
190 //=#include <boost/multiprecision/cpp_int.hpp>
191 //=#include <boost/random.hpp>
192 //=
193 //=int main()
194 //={
195 using namespace boost::multiprecision;
196 using namespace boost::random;
197 //
198 // Generate some distruted values:
199 //
200 uniform_real_distribution<cpp_bin_float_50> ur(-20, 20);
201 gamma_distribution<cpp_bin_float_50> gd(20);
202 independent_bits_engine<mt19937, std::numeric_limits<cpp_bin_float_50>::digits, cpp_int> gen;
203 //
204 // Generate some values:
205 //
206 std::cout << std::setprecision(50);
207 for(unsigned i = 0; i < 20; ++i)
208 std::cout << ur(gen) << std::endl;
209 for(unsigned i = 0; i < 20; ++i)
210 std::cout << gd(gen) << std::endl;
211 //= return 0;
212 //=}
213 //]
214 }
215
216 //[random_eg4_out
217 /*`
218 Which produces the following output:
219
220 [pre
221 -18.576837157065858312137736538355805944098004018928
222 4.5605477000094480453928920098152026546185388161216
223 -1.7611402252150150370944527411235180945558276280598
224 -2.471338289511354190492328039842914272146783953149
225 -7.4131520453411321647183692139916357315276121488316
226 -9.192739117661751364518299455475684051782402347659
227 7.0126880787149555595443325648941661436898526919013
228 2.8554749162054097111723076181877881960039268668423
229 14.390501287552165467965587841551705310012046701036
230 -8.9747073123748752412086051960748002945548570524149
231 -8.1305063133718605220959174700954037986278348616362
232 9.5496899464463627949564295930962040525540578754312
233 -15.309681742947663333436391348699943078942921692008
234 2.0454914298189175280771944784358385982869708951824
235 -10.069253024538932382193363493367304983742246396276
236 13.449212808583153116670057807764145176004060370818
237 -6.0065092542772507561228141992257782449634820245355
238 15.00971466974838379824678369267201922989930663822
239 16.158514812070905438581736305533045434508525979205
240 -2.1531361299576399413547008719541457739794964378093
241 19.398278792113040046930806838893737245011219380822
242 12.965216582396067073600685365545292876001524716225
243 19.561779374349650983983836397553672788578622096947
244 15.982213641588944604037715576313848977716540941271
245 23.96044616946856385664151481695038833903083043492
246 21.054716943622792848187523422423642819628010070375
247 18.596078774135209530930707331338838805575875990091
248 19.539530839287848627426769425090194390388333335812
249 17.176133236359396942946640290935498641489373354297
250 16.228802394876800099035133760539461530246286999827
251 23.63807160907473465631049083277558060813997674519
252 12.838499607321990428122225501321564153572478845401
253 16.878362445712403300584931374939967549572637230102
254 20.646246409377134464856282996941395597420615529803
255 16.602429236226052406561338766554127142762673418695
256 21.680007865714197450495711030406314524681744024329
257 21.038948660115771777833205901845639760348321521616
258 30.494499676527802078320016654058105593076348727966
259 18.704734464995637480940828829962787676146589788572
260 22.502216997171061548799304902323434654678156658236
261 ]
262 */
263 //]
264
265 void t5()
266 {
267 //[random_eg5
268 //=#include <boost/multiprecision/cpp_bin_float.hpp>
269 //=#include <boost/random.hpp>
270 //=#include <boost/scoped_ptr.hpp>
271 //=
272 //=int main()
273 //={
274 using namespace boost::multiprecision;
275 using namespace boost::random;
276 //
277 // Generate some multiprecision values, note that the generator is so large
278 // that we have to allocate it on the heap, otherwise we may run out of
279 // stack space! We could avoid this by using a floating point type which
280 // allocates it's internal storage on the heap - cpp_bin_float will do
281 // this with the correct template parameters, as will the GMP or MPFR
282 // based reals.
283 //
284 typedef lagged_fibonacci_01_engine<cpp_bin_float_50, 48, 44497, 21034 > big_fib_gen;
285 boost::scoped_ptr<big_fib_gen> pgen(new big_fib_gen);
286 //
287 // Generate some values:
288 //
289 std::cout << std::setprecision(50);
290 for(unsigned i = 0; i < 20; ++i)
291 std::cout << (*pgen)() << std::endl;
292 //
293 // try again with a ranlux generator, this is not quite so large
294 // so we can use the heap this time:
295 //
296 typedef subtract_with_carry_01_engine<cpp_bin_float_50, std::numeric_limits<cpp_bin_float_50>::digits - 5, 10, 24 > ranlux_big_base_01;
297 typedef discard_block_engine< ranlux_big_base_01, 389, 24 > big_ranlux;
298 big_ranlux rg;
299 for(unsigned i = 0; i < 20; ++i)
300 std::cout << rg() << std::endl;
301 //= return 0;
302 //=}
303 //]
304 }
305
306 int main()
307 {
308 t1();
309 t2();
310 t3();
311 t4();
312 t5();
313 return 0;
314 }
315