1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Generating Random Numbers</title> 5<link rel="stylesheet" href="../../multiprecision.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Multiprecision"> 8<link rel="up" href="../tut.html" title="Tutorial"> 9<link rel="prev" href="conversions.html" title="Constructing and Interconverting Between Number Types"> 10<link rel="next" href="primetest.html" title="Primality Testing"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="conversions.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="primetest.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_multiprecision.tut.random"></a><a class="link" href="random.html" title="Generating Random Numbers">Generating Random Numbers</a> 28</h3></div></div></div> 29<p> 30 Random numbers are generated in conjunction with Boost.Random. 31 </p> 32<p> 33 There is a single generator that supports generating random integers with 34 large bit counts: <a href="http://www.boost.org/doc/html/boost/random/independent_bits_engine.html" target="_top"><code class="computeroutput"><span class="identifier">independent_bits_engine</span></code></a>. This type 35 can be used with either <span class="emphasis"><em>unbounded</em></span> integer types, or 36 with <span class="emphasis"><em>bounded</em></span> (ie fixed precision) unsigned integers: 37 </p> 38<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_int</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 39<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">random</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 40 41<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 42<span class="special">{</span> 43 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span> 44 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">;</span> 45 46 <span class="comment">//</span> 47 <span class="comment">// Declare our random number generator type, the underlying generator</span> 48 <span class="comment">// is the Mersenne twister mt19937 engine, and we'll generate 256 bit</span> 49 <span class="comment">// random values, independent_bits_engine will make multiple calls</span> 50 <span class="comment">// to the underlying engine until we have the requested number of bits:</span> 51 <span class="comment">//</span> 52 <span class="keyword">typedef</span> <span class="identifier">independent_bits_engine</span><span class="special"><</span><span class="identifier">mt19937</span><span class="special">,</span> <span class="number">256</span><span class="special">,</span> <span class="identifier">cpp_int</span><span class="special">></span> <span class="identifier">generator_type</span><span class="special">;</span> 53 <span class="identifier">generator_type</span> <span class="identifier">gen</span><span class="special">;</span> 54 <span class="comment">//</span> 55 <span class="comment">// Generate some values:</span> 56 <span class="comment">//</span> 57 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">hex</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">showbase</span><span class="special">;</span> 58 <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 59 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">gen</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 60 <span class="comment">//</span> 61 <span class="comment">// Alternatively if we wish to generate random values in a fixed-precision</span> 62 <span class="comment">// type, then we must use an unsigned type in order to adhere to the</span> 63 <span class="comment">// conceptual requirements of the generator:</span> 64 <span class="comment">//</span> 65 <span class="keyword">typedef</span> <span class="identifier">independent_bits_engine</span><span class="special"><</span><span class="identifier">mt19937</span><span class="special">,</span> <span class="number">512</span><span class="special">,</span> <span class="identifier">uint512_t</span><span class="special">></span> <span class="identifier">generator512_type</span><span class="special">;</span> 66 <span class="identifier">generator512_type</span> <span class="identifier">gen512</span><span class="special">;</span> 67 <span class="comment">//</span> 68 <span class="comment">// Generate some 1024-bit unsigned values:</span> 69 <span class="comment">//</span> 70 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">hex</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">showbase</span><span class="special">;</span> 71 <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 72 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">gen512</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 73 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 74<span class="special">}</span> 75</pre> 76<p> 77 Program output is: 78 </p> 79<pre class="programlisting">0xD091BB5C22AE9EF6E7E1FAEED5C31F792082352CF807B7DFE9D300053895AFE1 800xA1E24BBA4EE4092B18F868638C16A625474BA8C43039CD1A8C006D5FFE2D7810 810xF51F2AE7FF1816E4F702EF59F7BADAFA285954A1B9D09511F878C4B3FB2A0137 820xF508E4AA1C1FE6527C419418CC50AA59CCDF2E5C4C0A1F3B2452A9DC01397D8D 830x6BF88C311CCA797AEA6DA4AEA3C78807CACE1969E0E0D4ADF5A14BAB80F00988 840xA7DE9F4CCC450CBA0924668F5C7DC380D96089C53640AC4CEF1A2E6DAE6D9426 850xADC1965B6613BA46C1FB41C2BD9B0ECDBE3DEDFC7989C8EE6468FD6E6C0DF032 860xA7CD66342C826D8B2BD2E4124D4A2DBEB4BF6FA7CC1A89590826328251097330 870x46E46CB0DF577EC20BD1E364262C556418DDA0C9FE7B45D9D2CE21C9D268409A 880xB1E049E1200BFA47512D6E73C3851EEEF341C0817D973E4808D17554A9E20D28 890xD091BB5C22AE9EF6E7E1FAEED5C31F792082352CF807B7DFE9D300053895AFE1A1E24BBA4EE4092B18F868638C16A625474BA8C43039CD1A8C006D5FFE2D7810 900xF51F2AE7FF1816E4F702EF59F7BADAFA285954A1B9D09511F878C4B3FB2A0137F508E4AA1C1FE6527C419418CC50AA59CCDF2E5C4C0A1F3B2452A9DC01397D8D 910x6BF88C311CCA797AEA6DA4AEA3C78807CACE1969E0E0D4ADF5A14BAB80F00988A7DE9F4CCC450CBA0924668F5C7DC380D96089C53640AC4CEF1A2E6DAE6D9426 920xADC1965B6613BA46C1FB41C2BD9B0ECDBE3DEDFC7989C8EE6468FD6E6C0DF032A7CD66342C826D8B2BD2E4124D4A2DBEB4BF6FA7CC1A89590826328251097330 930x46E46CB0DF577EC20BD1E364262C556418DDA0C9FE7B45D9D2CE21C9D268409AB1E049E1200BFA47512D6E73C3851EEEF341C0817D973E4808D17554A9E20D28 940x70518CE6203AC30361ADD0AB35D0430CC3F8E8920D1C8509CB92388E095436BF2FD6E20868A29AF97D61330B753EC6FC7211EFEA7CD15133A574C4FFCB41F198 950xB598EEF6EBBE7347C1332568CEBA5A7046A99459B4AD9F11AE00FEAA00B8B573A7B480B6B5F0B06C29A0EC27A4DAA0101E76A1C574BE91337F94C950C61F6ED6 960xF5B1C7A192E195F8572384D4E0732C8895D41B68CEE496C3394BBD52048CD47CC05309BED23D2D63414DE9C5D2229F23818666A3F0A8B109B2F6B12769A48341 970xE4123C566C548C8FF5941F6194B993AA8C1651342876763C237CE42EC300D11B263821CA3AEB820241EC0F84CF4AC36DD7393EE6FD0FC06A4118A30A551B54A4 980xD074F86F4CC1C54A3E57A70303774CDAEDE43895379CE62759988939E8490DDC325410E1D9352F6A4047080AF47C081D9DB51A85C765D71F79297527FCCA2773 99</pre> 100<p> 101 In addition, the generator adaptors <a href="http://www.boost.org/doc/html/boost/random/discard_block_engine.html" target="_top"><code class="computeroutput"><span class="identifier">discard_block</span></code></a>, <a href="http://www.boost.org/doc/html/boost/random/xor_combine_engine.html" target="_top"><code class="computeroutput"><span class="identifier">xor_combine_engine</span></code></a> and <a href="http://www.boost.org/doc/html/boost/random/discrete_distribution.html" target="_top"><code class="computeroutput"><span class="identifier">discrete_distribution</span></code></a> can be used 102 with multiprecision types. Note that if you seed an <code class="computeroutput"><span class="identifier">independent_bits_engine</span></code>, 103 then you are actually seeding the underlying generator, and should therefore 104 provide a sequence of unsigned 32-bit values as the seed. 105 </p> 106<p> 107 Alternatively we can generate integers in a given range using <a href="http://www.boost.org/doc/html/boost/random/uniform_int_distribution.html" target="_top"><code class="computeroutput"><span class="identifier">uniform_int_distribution</span></code></a>, this 108 will invoke the underlying engine multiple times to build up the required 109 number of bits in the result: 110 </p> 111<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_int</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 112<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">random</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 113 114<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 115<span class="special">{</span> 116 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span> 117 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">;</span> 118 119 <span class="comment">//</span> 120 <span class="comment">// Generate integers in a given range using uniform_int,</span> 121 <span class="comment">// the underlying generator is invoked multiple times</span> 122 <span class="comment">// to generate enough bits:</span> 123 <span class="comment">//</span> 124 <span class="identifier">mt19937</span> <span class="identifier">mt</span><span class="special">;</span> 125 <span class="identifier">uniform_int_distribution</span><span class="special"><</span><span class="identifier">cpp_int</span><span class="special">></span> <span class="identifier">ui</span><span class="special">(-(</span><span class="identifier">cpp_int</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special"><<</span> <span class="number">256</span><span class="special">),</span> <span class="identifier">cpp_int</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special"><<</span> <span class="number">256</span><span class="special">);</span> 126 <span class="comment">//</span> 127 <span class="comment">// Generate the numbers:</span> 128 <span class="comment">//</span> 129 <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 130 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">ui</span><span class="special">(</span><span class="identifier">mt</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 131 132 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 133<span class="special">}</span> 134</pre> 135<p> 136 Program output is 137 </p> 138<pre class="programlisting">25593993629538149833210527544371584707508847463356155903670894544241785158492 13912721121657520147247744796431842326146296294180809160027132416389225539366745 140106034929479008809862776424170460808190085984129117168803272987114325199071833 14186048861429530654936263414134573980939351899046345384016090167510299251354700 142-23473382144925885755951447143660880642389842563343761080591177733698450031250 14376840269649240973945508128641415259490679375154523618053296924666747244530145 14421638369166612496703991271955994563624044383325105383029306009417224944272131 14518829152205014764576551421737727569993966577957447887116062495161081023584880 146101521572847669971701030312596819435590097618913255156117898217707115132658117 147-97490271301923067621481012355971422109456300816856752380346627103308328292057 148</pre> 149<p> 150 It is also possible to use <a href="http://www.boost.org/doc/html/boost/random/uniform_int_distribution.html" target="_top"><code class="computeroutput"><span class="identifier">uniform_int_distribution</span></code></a> with a 151 multiprecision generator such as <a href="http://www.boost.org/doc/html/boost/random/independent_bits_engine.html" target="_top"><code class="computeroutput"><span class="identifier">independent_bits_engine</span></code></a>. Or to 152 use <a href="http://www.boost.org/doc/html/boost/random/uniform_smallint.html" target="_top"><code class="computeroutput"><span class="identifier">uniform_smallint</span></code></a> or <a href="http://www.boost.org/doc/html/boost/random/random_number_generator.html" target="_top"><code class="computeroutput"><span class="identifier">random_number_generator</span></code></a> with multiprecision 153 types. 154 </p> 155<p> 156 floating-point values in [0,1) are most easily generated using <a href="http://www.boost.org/doc/html/boost/random/generate_canonical.html" target="_top"><code class="computeroutput"><span class="identifier">generate_canonical</span></code></a>, note that 157 <code class="computeroutput"><span class="identifier">generate_canonical</span></code> will call 158 the generator multiple times to produce the requested number of bits, for 159 example we can use it with a regular generator like so: 160 </p> 161<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_bin_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 162<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">random</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 163 164<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 165<span class="special">{</span> 166 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span> 167 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">;</span> 168 169 <span class="identifier">mt19937</span> <span class="identifier">gen</span><span class="special">;</span> 170 <span class="comment">//</span> 171 <span class="comment">// Generate the values:</span> 172 <span class="comment">//</span> 173 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="number">50</span><span class="special">);</span> 174 <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="number">20</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 175 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">generate_canonical</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">>::</span><span class="identifier">digits</span><span class="special">>(</span><span class="identifier">gen</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 176 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 177<span class="special">}</span> 178</pre> 179<p> 180 Which produces the following output: 181 </p> 182<pre class="programlisting">0.96886777112423135248554451482797431507115448261086 1830.54722059636785192454525760726084778627750790023546 1840.99646132554800874317788284808573062871409279729804 1850.98110969177693891782396443737643892769773768718591 1860.29702944955795083040856753579705872634075574515969 1870.63976335709815275010379796044374742646738557798647 1880.79792861516022605265555700991255998690336456180995 1890.68135953856026596523755400091345037778580909233387 1900.47475868061723477935404326837783394169122045199915 1910.30191312687731969398296589840622989141067852863748 1920.87242882006730022427155209451091472382531795659709 1930.82190326480741096300318873712966555706035846579562 1940.49058903962146072778707295967429263659897501512813 1950.2102090745190061764133345429475530760261103345204 1960.4087311609617603484960794513055502599728804206333 1970.79397497154919267900450180642484943996546102712187 1980.70577425166871982574205252142383800792823003687121 1990.64396095652194035523385641523010248768636064728226 2000.5737546665965914620678634509134819579811035412969 2010.017773895576552474810236796736785695789752666554273 202</pre> 203<p> 204 Note however, the distributions do not invoke the generator multiple times 205 to fill up the mantissa of a multiprecision floating-point type with random 206 bits. For these therefore, we should probably use a multiprecision generator 207 (ie <code class="computeroutput"><span class="identifier">independent_bits_engine</span></code>) 208 in combination with the distribution: 209 </p> 210<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_bin_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 211<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_int</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 212<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">random</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 213 214<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 215<span class="special">{</span> 216 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span> 217 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">;</span> 218 <span class="comment">//</span> 219 <span class="comment">// Generate some distruted values:</span> 220 <span class="comment">//</span> 221 <span class="identifier">uniform_real_distribution</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">></span> <span class="identifier">ur</span><span class="special">(-</span><span class="number">20</span><span class="special">,</span> <span class="number">20</span><span class="special">);</span> 222 <span class="identifier">gamma_distribution</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">></span> <span class="identifier">gd</span><span class="special">(</span><span class="number">20</span><span class="special">);</span> 223 <span class="identifier">independent_bits_engine</span><span class="special"><</span><span class="identifier">mt19937</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">>::</span><span class="identifier">digits</span><span class="special">,</span> <span class="identifier">cpp_int</span><span class="special">></span> <span class="identifier">gen</span><span class="special">;</span> 224 <span class="comment">//</span> 225 <span class="comment">// Generate some values:</span> 226 <span class="comment">//</span> 227 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="number">50</span><span class="special">);</span> 228 <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="number">20</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 229 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">ur</span><span class="special">(</span><span class="identifier">gen</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 230 <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="number">20</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 231 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">gd</span><span class="special">(</span><span class="identifier">gen</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 232 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 233<span class="special">}</span> 234</pre> 235<p> 236 Which produces the following output: 237 </p> 238<pre class="programlisting">-18.576837157065858312137736538355805944098004018928 2394.5605477000094480453928920098152026546185388161216 240-1.7611402252150150370944527411235180945558276280598 241-2.471338289511354190492328039842914272146783953149 242-7.4131520453411321647183692139916357315276121488316 243-9.192739117661751364518299455475684051782402347659 2447.0126880787149555595443325648941661436898526919013 2452.8554749162054097111723076181877881960039268668423 24614.390501287552165467965587841551705310012046701036 247-8.9747073123748752412086051960748002945548570524149 248-8.1305063133718605220959174700954037986278348616362 2499.5496899464463627949564295930962040525540578754312 250-15.309681742947663333436391348699943078942921692008 2512.0454914298189175280771944784358385982869708951824 252-10.069253024538932382193363493367304983742246396276 25313.449212808583153116670057807764145176004060370818 254-6.0065092542772507561228141992257782449634820245355 25515.00971466974838379824678369267201922989930663822 25616.158514812070905438581736305533045434508525979205 257-2.1531361299576399413547008719541457739794964378093 25819.398278792113040046930806838893737245011219380822 25912.965216582396067073600685365545292876001524716225 26019.561779374349650983983836397553672788578622096947 26115.982213641588944604037715576313848977716540941271 26223.96044616946856385664151481695038833903083043492 26321.054716943622792848187523422423642819628010070375 26418.596078774135209530930707331338838805575875990091 26519.539530839287848627426769425090194390388333335812 26617.176133236359396942946640290935498641489373354297 26716.228802394876800099035133760539461530246286999827 26823.63807160907473465631049083277558060813997674519 26912.838499607321990428122225501321564153572478845401 27016.878362445712403300584931374939967549572637230102 27120.646246409377134464856282996941395597420615529803 27216.602429236226052406561338766554127142762673418695 27321.680007865714197450495711030406314524681744024329 27421.038948660115771777833205901845639760348321521616 27530.494499676527802078320016654058105593076348727966 27618.704734464995637480940828829962787676146589788572 27722.502216997171061548799304902323434654678156658236 278</pre> 279<p> 280 And finally, it is possible to use the floating-point generators <a href="http://www.boost.org/doc/html/boost/random/lagged_fibonacci_01_engine.html" target="_top"><code class="computeroutput"><span class="identifier">lagged_fibonacci_01_engine</span></code></a> and 281 <a href="http://www.boost.org/doc/html/boost/random/subtract_with_idp144360752.html" target="_top"><code class="computeroutput"><span class="identifier">subtract_with_carry_01_engine</span></code></a> directly 282 with multiprecision floating-point types. It's worth noting however, that 283 there is a distinct lack of literature on generating high bit-count random 284 numbers, and therefore a lack of "known good" parameters to use 285 with these generators in this situation. For this reason, these should probably 286 be used for research purposes only: 287 </p> 288<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_bin_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 289<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">random</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 290<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">scoped_ptr</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 291 292<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 293<span class="special">{</span> 294 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span> 295 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">;</span> 296 <span class="comment">//</span> 297 <span class="comment">// Generate some multiprecision values, note that the generator is so large</span> 298 <span class="comment">// that we have to allocate it on the heap, otherwise we may run out of</span> 299 <span class="comment">// stack space! We could avoid this by using a floating point type which</span> 300 <span class="comment">// allocates it's internal storage on the heap - cpp_bin_float will do</span> 301 <span class="comment">// this with the correct template parameters, as will the GMP or MPFR</span> 302 <span class="comment">// based reals.</span> 303 <span class="comment">//</span> 304 <span class="keyword">typedef</span> <span class="identifier">lagged_fibonacci_01_engine</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">,</span> <span class="number">48</span><span class="special">,</span> <span class="number">44497</span><span class="special">,</span> <span class="number">21034</span> <span class="special">></span> <span class="identifier">big_fib_gen</span><span class="special">;</span> 305 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">scoped_ptr</span><span class="special"><</span><span class="identifier">big_fib_gen</span><span class="special">></span> <span class="identifier">pgen</span><span class="special">(</span><span class="keyword">new</span> <span class="identifier">big_fib_gen</span><span class="special">);</span> 306 <span class="comment">//</span> 307 <span class="comment">// Generate some values:</span> 308 <span class="comment">//</span> 309 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="number">50</span><span class="special">);</span> 310 <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="number">20</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 311 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="special">(*</span><span class="identifier">pgen</span><span class="special">)()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 312 <span class="comment">//</span> 313 <span class="comment">// try again with a ranlux generator, this is not quite so large</span> 314 <span class="comment">// so we can use the heap this time:</span> 315 <span class="comment">//</span> 316 <span class="keyword">typedef</span> <span class="identifier">subtract_with_carry_01_engine</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">cpp_bin_float_50</span><span class="special">>::</span><span class="identifier">digits</span> <span class="special">-</span> <span class="number">5</span><span class="special">,</span> <span class="number">10</span><span class="special">,</span> <span class="number">24</span> <span class="special">></span> <span class="identifier">ranlux_big_base_01</span><span class="special">;</span> 317 <span class="keyword">typedef</span> <span class="identifier">discard_block_engine</span><span class="special"><</span> <span class="identifier">ranlux_big_base_01</span><span class="special">,</span> <span class="number">389</span><span class="special">,</span> <span class="number">24</span> <span class="special">></span> <span class="identifier">big_ranlux</span><span class="special">;</span> 318 <span class="identifier">big_ranlux</span> <span class="identifier">rg</span><span class="special">;</span> 319 <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special"><</span> <span class="number">20</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 320 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">rg</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 321 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 322<span class="special">}</span> 323</pre> 324</div> 325<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 326<td align="left"></td> 327<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John 328 Maddock and Christopher Kormanyos<p> 329 Distributed under the Boost Software License, Version 1.0. (See accompanying 330 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 331 </p> 332</div></td> 333</tr></table> 334<hr> 335<div class="spirit-nav"> 336<a accesskey="p" href="conversions.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="primetest.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 337</div> 338</body> 339</html> 340