]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/multiprecision/example/random_snips.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / multiprecision / example / random_snips.cpp
CommitLineData
7c673cae
FG
1///////////////////////////////////////////////////////////////
2// Copyright 2011 John Maddock. Distributed under the Boost
3// Software License, Version 1.0. (See accompanying file
92f5a8d4 4// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
7c673cae
FG
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
13void 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
600xD091BB5C22AE9EF6E7E1FAEED5C31F792082352CF807B7DFE9D300053895AFE1
610xA1E24BBA4EE4092B18F868638C16A625474BA8C43039CD1A8C006D5FFE2D7810
620xF51F2AE7FF1816E4F702EF59F7BADAFA285954A1B9D09511F878C4B3FB2A0137
630xF508E4AA1C1FE6527C419418CC50AA59CCDF2E5C4C0A1F3B2452A9DC01397D8D
640x6BF88C311CCA797AEA6DA4AEA3C78807CACE1969E0E0D4ADF5A14BAB80F00988
650xA7DE9F4CCC450CBA0924668F5C7DC380D96089C53640AC4CEF1A2E6DAE6D9426
660xADC1965B6613BA46C1FB41C2BD9B0ECDBE3DEDFC7989C8EE6468FD6E6C0DF032
670xA7CD66342C826D8B2BD2E4124D4A2DBEB4BF6FA7CC1A89590826328251097330
680x46E46CB0DF577EC20BD1E364262C556418DDA0C9FE7B45D9D2CE21C9D268409A
690xB1E049E1200BFA47512D6E73C3851EEEF341C0817D973E4808D17554A9E20D28
700xD091BB5C22AE9EF6E7E1FAEED5C31F792082352CF807B7DFE9D300053895AFE1A1E24BBA4EE4092B18F868638C16A625474BA8C43039CD1A8C006D5FFE2D7810
710xF51F2AE7FF1816E4F702EF59F7BADAFA285954A1B9D09511F878C4B3FB2A0137F508E4AA1C1FE6527C419418CC50AA59CCDF2E5C4C0A1F3B2452A9DC01397D8D
720x6BF88C311CCA797AEA6DA4AEA3C78807CACE1969E0E0D4ADF5A14BAB80F00988A7DE9F4CCC450CBA0924668F5C7DC380D96089C53640AC4CEF1A2E6DAE6D9426
730xADC1965B6613BA46C1FB41C2BD9B0ECDBE3DEDFC7989C8EE6468FD6E6C0DF032A7CD66342C826D8B2BD2E4124D4A2DBEB4BF6FA7CC1A89590826328251097330
740x46E46CB0DF577EC20BD1E364262C556418DDA0C9FE7B45D9D2CE21C9D268409AB1E049E1200BFA47512D6E73C3851EEEF341C0817D973E4808D17554A9E20D28
750x70518CE6203AC30361ADD0AB35D0430CC3F8E8920D1C8509CB92388E095436BF2FD6E20868A29AF97D61330B753EC6FC7211EFEA7CD15133A574C4FFCB41F198
760xB598EEF6EBBE7347C1332568CEBA5A7046A99459B4AD9F11AE00FEAA00B8B573A7B480B6B5F0B06C29A0EC27A4DAA0101E76A1C574BE91337F94C950C61F6ED6
770xF5B1C7A192E195F8572384D4E0732C8895D41B68CEE496C3394BBD52048CD47CC05309BED23D2D63414DE9C5D2229F23818666A3F0A8B109B2F6B12769A48341
780xE4123C566C548C8FF5941F6194B993AA8C1651342876763C237CE42EC300D11B263821CA3AEB820241EC0F84CF4AC36DD7393EE6FD0FC06A4118A30A551B54A4
790xD074F86F4CC1C54A3E57A70303774CDAEDE43895379CE62759988939E8490DDC325410E1D9352F6A4047080AF47C081D9DB51A85C765D71F79297527FCCA2773
80]
81*/
82//]
83
84
85void 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/*`
116Program output is
117
118[pre
11925593993629538149833210527544371584707508847463356155903670894544241785158492
12012721121657520147247744796431842326146296294180809160027132416389225539366745
121106034929479008809862776424170460808190085984129117168803272987114325199071833
12286048861429530654936263414134573980939351899046345384016090167510299251354700
123-23473382144925885755951447143660880642389842563343761080591177733698450031250
12476840269649240973945508128641415259490679375154523618053296924666747244530145
12521638369166612496703991271955994563624044383325105383029306009417224944272131
12618829152205014764576551421737727569993966577957447887116062495161081023584880
127101521572847669971701030312596819435590097618913255156117898217707115132658117
128-97490271301923067621481012355971422109456300816856752380346627103308328292057
129]
130*/
131//]
132
133void 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/*`
158Which produces the following output:
159
160[pre
1610.96886777112423135248554451482797431507115448261086
1620.54722059636785192454525760726084778627750790023546
1630.99646132554800874317788284808573062871409279729804
1640.98110969177693891782396443737643892769773768718591
1650.29702944955795083040856753579705872634075574515969
1660.63976335709815275010379796044374742646738557798647
1670.79792861516022605265555700991255998690336456180995
1680.68135953856026596523755400091345037778580909233387
1690.47475868061723477935404326837783394169122045199915
1700.30191312687731969398296589840622989141067852863748
1710.87242882006730022427155209451091472382531795659709
1720.82190326480741096300318873712966555706035846579562
1730.49058903962146072778707295967429263659897501512813
1740.2102090745190061764133345429475530760261103345204
1750.4087311609617603484960794513055502599728804206333
1760.79397497154919267900450180642484943996546102712187
1770.70577425166871982574205252142383800792823003687121
1780.64396095652194035523385641523010248768636064728226
1790.5737546665965914620678634509134819579811035412969
1800.017773895576552474810236796736785695789752666554273
181]
182*/
183//]
184
185void 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/*`
218Which produces the following output:
219
220[pre
221-18.576837157065858312137736538355805944098004018928
2224.5605477000094480453928920098152026546185388161216
223-1.7611402252150150370944527411235180945558276280598
224-2.471338289511354190492328039842914272146783953149
225-7.4131520453411321647183692139916357315276121488316
226-9.192739117661751364518299455475684051782402347659
2277.0126880787149555595443325648941661436898526919013
2282.8554749162054097111723076181877881960039268668423
22914.390501287552165467965587841551705310012046701036
230-8.9747073123748752412086051960748002945548570524149
231-8.1305063133718605220959174700954037986278348616362
2329.5496899464463627949564295930962040525540578754312
233-15.309681742947663333436391348699943078942921692008
2342.0454914298189175280771944784358385982869708951824
235-10.069253024538932382193363493367304983742246396276
23613.449212808583153116670057807764145176004060370818
237-6.0065092542772507561228141992257782449634820245355
23815.00971466974838379824678369267201922989930663822
23916.158514812070905438581736305533045434508525979205
240-2.1531361299576399413547008719541457739794964378093
24119.398278792113040046930806838893737245011219380822
24212.965216582396067073600685365545292876001524716225
24319.561779374349650983983836397553672788578622096947
24415.982213641588944604037715576313848977716540941271
24523.96044616946856385664151481695038833903083043492
24621.054716943622792848187523422423642819628010070375
24718.596078774135209530930707331338838805575875990091
24819.539530839287848627426769425090194390388333335812
24917.176133236359396942946640290935498641489373354297
25016.228802394876800099035133760539461530246286999827
25123.63807160907473465631049083277558060813997674519
25212.838499607321990428122225501321564153572478845401
25316.878362445712403300584931374939967549572637230102
25420.646246409377134464856282996941395597420615529803
25516.602429236226052406561338766554127142762673418695
25621.680007865714197450495711030406314524681744024329
25721.038948660115771777833205901845639760348321521616
25830.494499676527802078320016654058105593076348727966
25918.704734464995637480940828829962787676146589788572
26022.502216997171061548799304902323434654678156658236
261]
262*/
263//]
264
265void 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
306int main()
307{
308 t1();
309 t2();
310 t3();
311 t4();
312 t5();
313 return 0;
314}
315