<div class="gmail_extra"><div class="gmail_quote">On Tue, Apr 24, 2012 at 3:02 PM, Błażej Jaworowski <span dir="ltr">&lt;<a href="mailto:jmvarelse@gmail.com" target="_blank">jmvarelse@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>I have a Monte Carlo simulation program (Ising model) in which I wanted to use Boost.Random number generators instead of one from <i>Numerical Recipes. </i>It works when the generator is called only in main, but when I use it inside function, I have &quot;identifier not found&quot;. I don&#39;t want to initialize them each time I call a function because this will repeat the sequences of random number. Is there a way to overcome it?</div>

<div>I don&#39;t know much about typedefs and templates, so my initialization of the generator is just copied from some tutorial and changed in, I hope, appropriate places. </div><div>Here is the example code (I wouldn&#39;t paste original program, it&#39;s too long, but here is the same problem)</div>

<div><br></div><div>// random_test.cpp : Defines the entry point for the console application.<br>//<br><br>#include &quot;stdafx.h&quot;<br>#include &quot;iostream&quot;<br>#include &lt;ctime&gt;<br>#include &lt;boost/random.hpp&gt;<br>

using namespace std;<br>namespace boost {<br>  namespace random {<br>    template&lt;typename UIntType, int w, unsigned int p, unsigned int q&gt; <br>      class lagged_fibonacci_engine;<br>    template&lt;typename RealType, int w, unsigned int p, unsigned int q&gt; <br>

      class lagged_fibonacci_01_engine;<br>    typedef lagged_fibonacci_01_engine&lt; double, 48, 607, 273 &gt; lagged_fibonacci607;<br>    typedef lagged_fibonacci_01_engine&lt; double, 48, 1279, 418 &gt; lagged_fibonacci1279;<br>

    typedef lagged_fibonacci_01_engine&lt; double, 48, 2281, 1252 &gt; lagged_fibonacci2281;<br>    typedef lagged_fibonacci_01_engine&lt; double, 48, 3217, 576 &gt; lagged_fibonacci3217;<br>    typedef lagged_fibonacci_01_engine&lt; double, 48, 4423, 2098 &gt; lagged_fibonacci4423;<br>

    typedef lagged_fibonacci_01_engine&lt; double, 48, 9689, 5502 &gt; lagged_fibonacci9689;<br>    typedef lagged_fibonacci_01_engine&lt; double, 48, 19937, 9842 &gt; lagged_fibonacci19937;<br>    typedef lagged_fibonacci_01_engine&lt; double, 48, 23209, 13470 &gt; lagged_fibonacci23209;<br>

    typedef lagged_fibonacci_01_engine&lt; double, 48, 44497, 21034 &gt; lagged_fibonacci44497;<br>  }<br>}<br><br>void generate();</div><div><br>int _tmain(int argc, _TCHAR* argv[])<br>{const int rangeMin = 1; <br>const int rangeMax = 10; <br>

typedef boost::uniform_real&lt;&gt; NumberDistribution; <br>typedef boost::random::lagged_fibonacci9689 RandomNumberGenerator; <br>  typedef boost::variate_generator&lt;RandomNumberGenerator&amp;, <br>                                   NumberDistribution&gt; Generator;<br>

  NumberDistribution distribution(rangeMin, rangeMax); <br>  RandomNumberGenerator generator; <br>  Generator numberGenerator(generator, distribution); <br>  generator.seed(time(0)); // seed with the current time <br> generate();<br>

	return 0;<br>}<br>void generate(){cout &lt;&lt; numberGenerator() &lt;&lt; endl;}</div></blockquote><div><br>Why don&#39;t you just pass a reference to your numberGenerator object to your generate() function?<br><br>- Jeff<br>
<br></div></div></div>