Boost logo

Boost Users :

Subject: Re: [Boost-users] [Random] Noob question: Random number generators inside function
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2012-04-25 02:44:32


On Tue, Apr 24, 2012 at 3:02 PM, Błażej Jaworowski <jmvarelse_at_[hidden]>wrote:

> I have a Monte Carlo simulation program (Ising model) in which I wanted to
> use Boost.Random number generators instead of one from *Numerical
> Recipes. *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?
> 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.
> Here is the example code (I wouldn't paste original program, it's too
> long, but here is the same problem)
>
> // random_test.cpp : Defines the entry point for the console application.
> //
>
> #include "stdafx.h"
> #include "iostream"
> #include <ctime>
> #include <boost/random.hpp>
> using namespace std;
> namespace boost {
> namespace random {
> template<typename UIntType, int w, unsigned int p, unsigned int q>
> class lagged_fibonacci_engine;
> template<typename RealType, int w, unsigned int p, unsigned int q>
> class lagged_fibonacci_01_engine;
> typedef lagged_fibonacci_01_engine< double, 48, 607, 273 >
> lagged_fibonacci607;
> typedef lagged_fibonacci_01_engine< double, 48, 1279, 418 >
> lagged_fibonacci1279;
> typedef lagged_fibonacci_01_engine< double, 48, 2281, 1252 >
> lagged_fibonacci2281;
> typedef lagged_fibonacci_01_engine< double, 48, 3217, 576 >
> lagged_fibonacci3217;
> typedef lagged_fibonacci_01_engine< double, 48, 4423, 2098 >
> lagged_fibonacci4423;
> typedef lagged_fibonacci_01_engine< double, 48, 9689, 5502 >
> lagged_fibonacci9689;
> typedef lagged_fibonacci_01_engine< double, 48, 19937, 9842 >
> lagged_fibonacci19937;
> typedef lagged_fibonacci_01_engine< double, 48, 23209, 13470 >
> lagged_fibonacci23209;
> typedef lagged_fibonacci_01_engine< double, 48, 44497, 21034 >
> lagged_fibonacci44497;
> }
> }
>
> void generate();
>
> int _tmain(int argc, _TCHAR* argv[])
> {const int rangeMin = 1;
> const int rangeMax = 10;
> typedef boost::uniform_real<> NumberDistribution;
> typedef boost::random::lagged_fibonacci9689 RandomNumberGenerator;
> typedef boost::variate_generator<RandomNumberGenerator&,
> NumberDistribution> Generator;
> NumberDistribution distribution(rangeMin, rangeMax);
> RandomNumberGenerator generator;
> Generator numberGenerator(generator, distribution);
> generator.seed(time(0)); // seed with the current time
> generate();
> return 0;
> }
> void generate(){cout << numberGenerator() << endl;}
>

Why don't you just pass a reference to your numberGenerator object to your
generate() function?

- Jeff



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net