<div class="gmail_extra"><div class="gmail_quote">On Tue, Apr 24, 2012 at 3:02 PM, Błażej Jaworowski <span dir="ltr"><<a href="mailto:jmvarelse@gmail.com" target="_blank">jmvarelse@gmail.com</a>></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 "identifier not found". I don'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'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't paste original program, it'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 "stdafx.h"<br>#include "iostream"<br>#include <ctime><br>#include <boost/random.hpp><br> using namespace std;<br>namespace boost {<br> namespace random {<br> template<typename UIntType, int w, unsigned int p, unsigned int q> <br> class lagged_fibonacci_engine;<br> template<typename RealType, int w, unsigned int p, unsigned int q> <br> class lagged_fibonacci_01_engine;<br> typedef lagged_fibonacci_01_engine< double, 48, 607, 273 > lagged_fibonacci607;<br> typedef lagged_fibonacci_01_engine< double, 48, 1279, 418 > lagged_fibonacci1279;<br> typedef lagged_fibonacci_01_engine< double, 48, 2281, 1252 > lagged_fibonacci2281;<br> typedef lagged_fibonacci_01_engine< double, 48, 3217, 576 > lagged_fibonacci3217;<br> typedef lagged_fibonacci_01_engine< double, 48, 4423, 2098 > lagged_fibonacci4423;<br> typedef lagged_fibonacci_01_engine< double, 48, 9689, 5502 > lagged_fibonacci9689;<br> typedef lagged_fibonacci_01_engine< double, 48, 19937, 9842 > lagged_fibonacci19937;<br> typedef lagged_fibonacci_01_engine< double, 48, 23209, 13470 > lagged_fibonacci23209;<br> typedef lagged_fibonacci_01_engine< double, 48, 44497, 21034 > 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<> NumberDistribution; <br>typedef boost::random::lagged_fibonacci9689 RandomNumberGenerator; <br> typedef boost::variate_generator<RandomNumberGenerator&, <br> NumberDistribution> 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 << numberGenerator() << endl;}</div></blockquote><div><br>Why don't you just pass a reference to your numberGenerator object to your generate() function?<br><br>- Jeff<br> <br></div></div></div>