random_snips.cpp 11 KB

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