1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Tutorial</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../boost_random.html" title="Chapter 33. Boost.Random"> 10<link rel="prev" href="../boost_random.html" title="Chapter 33. Boost.Random"> 11<link rel="next" href="reference.html" title="Reference"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="../boost_random.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_random.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="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="boost_random.tutorial"></a><a class="link" href="tutorial.html" title="Tutorial">Tutorial</a> 29</h2></div></div></div> 30<div class="toc"><dl class="toc"> 31<dt><span class="section"><a href="tutorial.html#boost_random.tutorial.generating_integers_in_a_range">Generating 32 integers in a range</a></span></dt> 33<dt><span class="section"><a href="tutorial.html#boost_random.tutorial.generating_integers_with_different_probabilities">Generating 34 integers with different probabilities</a></span></dt> 35<dt><span class="section"><a href="tutorial.html#boost_random.tutorial.generating_a_random_password">Generating 36 a random password</a></span></dt> 37<dt><span class="section"><a href="tutorial.html#boost_random.tutorial.generating_quasi_random_line_sphere_intersections">Generating 38 quasi-random line-sphere intersections</a></span></dt> 39</dl></div> 40<div class="section"> 41<div class="titlepage"><div><div><h3 class="title"> 42<a name="boost_random.tutorial.generating_integers_in_a_range"></a><a class="link" href="tutorial.html#boost_random.tutorial.generating_integers_in_a_range" title="Generating integers in a range">Generating 43 integers in a range</a> 44</h3></div></div></div> 45<p> 46 For the source of this example see <a href="../../..//libs/random/example/die.cpp" target="_top">die.cpp</a>. 47 First we include the headers we need for <code class="computeroutput"><a class="link" href="../boost/random/mt19937.html" title="Type definition mt19937">mt19937</a></code> 48 and <code class="computeroutput"><a class="link" href="../boost/random/uniform_int_distribution.html" title="Class template uniform_int_distribution">uniform_int_distribution</a></code>. 49 </p> 50<p> 51</p> 52<pre class="programlisting"><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">mersenne_twister</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 53<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">uniform_int_distribution</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 54</pre> 55<p> 56 </p> 57<p> 58 We use <code class="computeroutput"><a class="link" href="../boost/random/mt19937.html" title="Type definition mt19937">mt19937</a></code> with the 59 default seed as a source of randomness. The numbers produced will be the 60 same every time the program is run. One common method to change this is to 61 seed with the current time (<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">time</span><span class="special">(</span><span class="number">0</span><span class="special">)</span></code> 62 defined in ctime). 63 </p> 64<p> 65</p> 66<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">::</span><span class="identifier">mt19937</span> <span class="identifier">gen</span><span class="special">;</span> 67</pre> 68<p> 69 </p> 70<div class="note"><table border="0" summary="Note"> 71<tr> 72<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td> 73<th align="left">Note</th> 74</tr> 75<tr><td align="left" valign="top"><p> 76 We are using a <span class="emphasis"><em>global</em></span> generator object here. This 77 is important because we don't want to create a new <a class="link" href="reference.html#boost_random.reference.concepts.pseudo_random_number_generator" title="Pseudo-Random Number Generator">pseudo-random 78 number generator</a> at every call 79 </p></td></tr> 80</table></div> 81<p> 82 Now we can define a function that simulates an ordinary six-sided die. 83 </p> 84<p> 85</p> 86<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">roll_die</span><span class="special">()</span> <span class="special">{</span> 87 <a class="co" name="boost_random.tutorial.generating_integers_in_a_range.c0" href="tutorial.html#boost_random.tutorial.generating_integers_in_a_range.c1"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a><span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">::</span><span class="identifier">uniform_int_distribution</span><span class="special"><></span> <span class="identifier">dist</span><span class="special">(</span><span class="number">1</span><span class="special">,</span> <span class="number">6</span><span class="special">);</span> 88 <a class="co" name="boost_random.tutorial.generating_integers_in_a_range.c2" href="tutorial.html#boost_random.tutorial.generating_integers_in_a_range.c3"><img src="../../../doc/src/images/callouts/2.png" alt="2" border="0"></a><span class="keyword">return</span> <span class="identifier">dist</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span> 89<span class="special">}</span> 90</pre> 91<p> 92 </p> 93<div class="calloutlist"><table border="0" summary="Callout list"> 94<tr> 95<td width="5%" valign="top" align="left"><p><a name="boost_random.tutorial.generating_integers_in_a_range.c1"></a><a href="#boost_random.tutorial.generating_integers_in_a_range.c0"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> 96<td valign="top" align="left"> 97<p> 98 <code class="computeroutput"><a class="link" href="../boost/random/mt19937.html" title="Type definition mt19937">mt19937</a></code> produces 99 integers in the range [0, 2<sup>32</sup>-1]. However, we want numbers in the range 100 [1, 6]. The distribution <code class="computeroutput"><a class="link" href="../boost/random/uniform_int_distribution.html" title="Class template uniform_int_distribution">uniform_int_distribution</a></code> 101 performs this transformation. 102 </p> 103<div class="warning"><table border="0" summary="Warning"> 104<tr> 105<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../doc/src/images/warning.png"></td> 106<th align="left">Warning</th> 107</tr> 108<tr><td align="left" valign="top"><p> 109 Contrary to common C++ usage <code class="computeroutput"><a class="link" href="../boost/random/uniform_int_distribution.html" title="Class template uniform_int_distribution">uniform_int_distribution</a></code> 110 does not take a <span class="emphasis"><em>half-open range</em></span>. Instead it takes 111 a <span class="emphasis"><em>closed range</em></span>. Given the parameters 1 and 6, 112 <code class="computeroutput"><a class="link" href="../boost/random/uniform_int_distribution.html" title="Class template uniform_int_distribution">uniform_int_distribution</a></code> 113 can produce any of the values 1, 2, 3, 4, 5, or 6. 114 </p></td></tr> 115</table></div> 116</td> 117</tr> 118<tr> 119<td width="5%" valign="top" align="left"><p><a name="boost_random.tutorial.generating_integers_in_a_range.c3"></a><a href="#boost_random.tutorial.generating_integers_in_a_range.c2"><img src="../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td> 120<td valign="top" align="left"><p> 121 A distribution is a function object. We generate a random number by calling 122 <code class="computeroutput"><span class="identifier">dist</span></code> with the generator. 123 </p></td> 124</tr> 125</table></div> 126</div> 127<div class="section"> 128<div class="titlepage"><div><div><h3 class="title"> 129<a name="boost_random.tutorial.generating_integers_with_different_probabilities"></a><a class="link" href="tutorial.html#boost_random.tutorial.generating_integers_with_different_probabilities" title="Generating integers with different probabilities">Generating 130 integers with different probabilities</a> 131</h3></div></div></div> 132<p> 133 For the source of this example see <a href="../../..//libs/random/example/weighted_die.cpp" target="_top">weighted_die.cpp</a>. 134 </p> 135<p> 136</p> 137<pre class="programlisting"><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">mersenne_twister</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 138<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">discrete_distribution</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 139 140<span class="identifier">boost</span><span class="special">::</span><span class="identifier">mt19937</span> <span class="identifier">gen</span><span class="special">;</span> 141</pre> 142<p> 143 </p> 144<p> 145 This time, instead of a fair die, the probability of rolling a 1 is 50% (!). 146 The other five faces are all equally likely. 147 </p> 148<p> 149 <code class="computeroutput"><a class="link" href="../boost/random/discrete_distribution.html" title="Class template discrete_distribution">discrete_distribution</a></code> 150 works nicely here by allowing us to assign weights to each of the possible 151 outcomes. 152 </p> 153<div class="tip"><table border="0" summary="Tip"> 154<tr> 155<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../doc/src/images/tip.png"></td> 156<th align="left">Tip</th> 157</tr> 158<tr><td align="left" valign="top"><p> 159 If your compiler supports <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">initializer_list</span></code>, 160 you can initialize <code class="computeroutput"><a class="link" href="../boost/random/discrete_distribution.html" title="Class template discrete_distribution">discrete_distribution</a></code> 161 directly with the weights. 162 </p></td></tr> 163</table></div> 164<p> 165</p> 166<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">probabilities</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span> 167 <span class="number">0.5</span><span class="special">,</span> <span class="number">0.1</span><span class="special">,</span> <span class="number">0.1</span><span class="special">,</span> <span class="number">0.1</span><span class="special">,</span> <span class="number">0.1</span><span class="special">,</span> <span class="number">0.1</span> 168<span class="special">};</span> 169<span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">::</span><span class="identifier">discrete_distribution</span><span class="special"><></span> <span class="identifier">dist</span><span class="special">(</span><span class="identifier">probabilities</span><span class="special">);</span> 170</pre> 171<p> 172 </p> 173<p> 174 Now define a function that simulates rolling this die. 175 </p> 176<p> 177</p> 178<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">roll_weighted_die</span><span class="special">()</span> <span class="special">{</span> 179 <a class="co" name="boost_random.tutorial.generating_integers_with_different_probabilities.c0" href="tutorial.html#boost_random.tutorial.generating_integers_with_different_probabilities.c1"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a><span class="keyword">return</span> <span class="identifier">dist</span><span class="special">(</span><span class="identifier">gen</span><span class="special">)</span> <span class="special">+</span> <span class="number">1</span><span class="special">;</span> 180<span class="special">}</span> 181</pre> 182<p> 183 </p> 184<div class="calloutlist"><table border="0" summary="Callout list"><tr> 185<td width="5%" valign="top" align="left"><p><a name="boost_random.tutorial.generating_integers_with_different_probabilities.c1"></a><a href="#boost_random.tutorial.generating_integers_with_different_probabilities.c0"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> 186<td valign="top" align="left"><p> 187 Add 1 to make sure that the result is in the range [1,6] instead of [0,5]. 188 </p></td> 189</tr></table></div> 190</div> 191<div class="section"> 192<div class="titlepage"><div><div><h3 class="title"> 193<a name="boost_random.tutorial.generating_a_random_password"></a><a class="link" href="tutorial.html#boost_random.tutorial.generating_a_random_password" title="Generating a random password">Generating 194 a random password</a> 195</h3></div></div></div> 196<p> 197 For the source of this example see <a href="../../..//libs/random/example/password.cpp" target="_top">password.cpp</a>. 198 </p> 199<p> 200 This example demonstrates generating a random 8 character password. 201 </p> 202<p> 203</p> 204<pre class="programlisting"><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">random_device</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 205<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">uniform_int_distribution</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 206<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 207 208<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span> 209 <a class="co" name="boost_random.tutorial.generating_a_random_password.c0" href="tutorial.html#boost_random.tutorial.generating_a_random_password.c1"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">chars</span><span class="special">(</span> 210 <span class="string">"abcdefghijklmnopqrstuvwxyz"</span> 211 <span class="string">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span> 212 <span class="string">"1234567890"</span> 213 <span class="string">"!@#$%^&*()"</span> 214 <span class="string">"`~-_=+[{]}\\|;:'\",<.>/? "</span><span class="special">);</span> 215 <a class="co" name="boost_random.tutorial.generating_a_random_password.c2" href="tutorial.html#boost_random.tutorial.generating_a_random_password.c3"><img src="../../../doc/src/images/callouts/2.png" alt="2" border="0"></a><span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">::</span><span class="identifier">random_device</span> <span class="identifier">rng</span><span class="special">;</span> 216 <a class="co" name="boost_random.tutorial.generating_a_random_password.c4" href="tutorial.html#boost_random.tutorial.generating_a_random_password.c5"><img src="../../../doc/src/images/callouts/3.png" alt="3" border="0"></a><span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">::</span><span class="identifier">uniform_int_distribution</span><span class="special"><></span> <span class="identifier">index_dist</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">chars</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span> 217 <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</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">8</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span> 218 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">chars</span><span class="special">[</span><span class="identifier">index_dist</span><span class="special">(</span><span class="identifier">rng</span><span class="special">)];</span> 219 <span class="special">}</span> 220 <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">endl</span><span class="special">;</span> 221<span class="special">}</span> 222</pre> 223<p> 224 </p> 225<div class="calloutlist"><table border="0" summary="Callout list"> 226<tr> 227<td width="5%" valign="top" align="left"><p><a name="boost_random.tutorial.generating_a_random_password.c1"></a><a href="#boost_random.tutorial.generating_a_random_password.c0"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> 228<td valign="top" align="left"><p> 229 We first define the characters that we're going to allow. This is pretty 230 much just the characters on a standard keyboard. 231 </p></td> 232</tr> 233<tr> 234<td width="5%" valign="top" align="left"><p><a name="boost_random.tutorial.generating_a_random_password.c3"></a><a href="#boost_random.tutorial.generating_a_random_password.c2"><img src="../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td> 235<td valign="top" align="left"><p> 236 We use <code class="computeroutput"><a class="link" href="../boost/random/random_device.html" title="Class random_device">random_device</a></code> 237 as a source of entropy, since we want passwords that are not predictable. 238 </p></td> 239</tr> 240<tr> 241<td width="5%" valign="top" align="left"><p><a name="boost_random.tutorial.generating_a_random_password.c5"></a><a href="#boost_random.tutorial.generating_a_random_password.c4"><img src="../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td> 242<td valign="top" align="left"><p> 243 Finally we select 8 random characters from the string and print them 244 to cout. 245 </p></td> 246</tr> 247</table></div> 248</div> 249<div class="section"> 250<div class="titlepage"><div><div><h3 class="title"> 251<a name="boost_random.tutorial.generating_quasi_random_line_sphere_intersections"></a><a class="link" href="tutorial.html#boost_random.tutorial.generating_quasi_random_line_sphere_intersections" title="Generating quasi-random line-sphere intersections">Generating 252 quasi-random line-sphere intersections</a> 253</h3></div></div></div> 254<p> 255 For the source of this example see <a href="../../..//libs/random/example/intersections.cpp" target="_top">intersections.cpp</a>. 256 </p> 257<p> 258 This example demonstrates generating quasi-randomly distributed chord entry 259 and exit points on an S<sup>2</sup> sphere. 260 </p> 261<p> 262 First we include the headers we need for <code class="computeroutput"><a class="link" href="../boost/random/niederreiter_base2.html" title="Type definition niederreiter_base2">niederreiter_base2</a></code> 263 and <code class="computeroutput"><a class="link" href="../boost/random/uniform_01.html" title="Class template uniform_01">uniform_01</a></code> distribution. 264 </p> 265<p> 266</p> 267<pre class="programlisting"><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">niederreiter_base2</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 268<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">uniform_01</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 269 270<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">constants</span><span class="special">/</span><span class="identifier">constants</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 271 272<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">tuple</span><span class="special">/</span><span class="identifier">tuple</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 273</pre> 274<p> 275 </p> 276<p> 277 We use 4-dimensional <code class="computeroutput"><a class="link" href="../boost/random/niederreiter_base2.html" title="Type definition niederreiter_base2">niederreiter_base2</a></code> 278 as a source of randomness. 279 </p> 280<p> 281</p> 282<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">::</span><span class="identifier">niederreiter_base2</span> <span class="identifier">gen</span><span class="special">(</span><span class="number">4</span><span class="special">);</span> 283 284 285<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 286<span class="special">{</span> 287 <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">tuple</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">></span> <span class="identifier">point_t</span><span class="special">;</span> 288 289 <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">n_points</span> <span class="special">=</span> <span class="number">100</span><span class="special">;</span> <span class="comment">// we will generate 100 points</span> 290 291 <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">point_t</span><span class="special">></span> <span class="identifier">points</span><span class="special">;</span> 292 <span class="identifier">points</span><span class="special">.</span><span class="identifier">reserve</span><span class="special">(</span><span class="identifier">n_points</span><span class="special">);</span> 293 294 <a class="co" name="boost_random.tutorial.generating_quasi_random_line_sphere_intersections.c0" href="tutorial.html#boost_random.tutorial.generating_quasi_random_line_sphere_intersections.c1"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a><span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">::</span><span class="identifier">uniform_01</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="identifier">dist</span><span class="special">;</span> 295 296 <span class="keyword">for</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</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="identifier">n_points</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 297 <span class="special">{</span> 298</pre> 299<p> 300 </p> 301<p> 302 Using formula from J. Rovira et al., "Point sampling with uniformly 303 distributed lines", 2005 to compute uniformly distributed chord entry 304 and exit points on the surface of a sphere. 305 </p> 306<p> 307</p> 308<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">cos_theta</span> <span class="special">=</span> <span class="number">1</span> <span class="special">-</span> <span class="number">2</span> <span class="special">*</span> <span class="identifier">dist</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span> 309<span class="keyword">double</span> <span class="identifier">sin_theta</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">sqrt</span><span class="special">(</span><span class="number">1</span> <span class="special">-</span> <span class="identifier">cos_theta</span> <span class="special">*</span> <span class="identifier">cos_theta</span><span class="special">);</span> 310<span class="keyword">double</span> <span class="identifier">phi</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">constants</span><span class="special">::</span><span class="identifier">two_pi</span><span class="special"><</span><span class="keyword">double</span><span class="special">>()</span> <span class="special">*</span> <span class="identifier">dist</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span> 311<span class="keyword">double</span> <span class="identifier">sin_phi</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">sin</span><span class="special">(</span><span class="identifier">phi</span><span class="special">),</span> <span class="identifier">cos_phi</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cos</span><span class="special">(</span><span class="identifier">phi</span><span class="special">);</span> 312 313<span class="identifier">point_t</span> <span class="identifier">point_on_sphere</span><span class="special">(</span><span class="identifier">sin_theta</span><span class="special">*</span><span class="identifier">sin_phi</span><span class="special">,</span> <span class="identifier">cos_theta</span><span class="special">,</span> <span class="identifier">sin_theta</span><span class="special">*</span><span class="identifier">cos_phi</span><span class="special">);</span> 314</pre> 315<p> 316 </p> 317<p> 318 Here we assume that our sphere is a unit sphere at origin. If your sphere 319 was different then now would be the time to scale and translate the <code class="computeroutput"><span class="identifier">point_on_sphere</span></code>. 320 </p> 321<p> 322</p> 323<pre class="programlisting"> <span class="identifier">points</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">point_on_sphere</span><span class="special">);</span> 324<span class="special">}</span> 325</pre> 326<p> 327 </p> 328<p> 329 Vector <code class="computeroutput"><span class="identifier">points</span></code> now holds generated 330 3D points on a sphere. 331 </p> 332<p> 333</p> 334<pre class="programlisting"> <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 335<span class="special">}</span> 336</pre> 337<p> 338 </p> 339<div class="calloutlist"><table border="0" summary="Callout list"><tr> 340<td width="5%" valign="top" align="left"><p><a name="boost_random.tutorial.generating_quasi_random_line_sphere_intersections.c1"></a><a href="#boost_random.tutorial.generating_quasi_random_line_sphere_intersections.c0"><img src="../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td> 341<td valign="top" align="left"><p> 342 <code class="computeroutput"><a class="link" href="../boost/random/niederreiter_base2.html" title="Type definition niederreiter_base2">niederreiter_base2</a></code> 343 produces integers in the range [0, 2<sup>64</sup>-1]. However, we want numbers in 344 the range [0, 1). The distribution <code class="computeroutput"><a class="link" href="../boost/random/uniform_01.html" title="Class template uniform_01">uniform_01</a></code> 345 performs this transformation. 346 </p></td> 347</tr></table></div> 348</div> 349</div> 350<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 351<td align="left"></td> 352<td align="right"><div class="copyright-footer">Copyright © 2000-2005 Jens Maurer<br>Copyright © 2009, 2010 Steven Watanabe<p> 353 Distributed under the Boost Software License, Version 1.0. (See accompanying 354 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>) 355 </p> 356</div></td> 357</tr></table> 358<hr> 359<div class="spirit-nav"> 360<a accesskey="p" href="../boost_random.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_random.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="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 361</div> 362</body> 363</html> 364