S
StephQ
I'm facing the following problem: I'm using the Gnu Scientific Library
(it is a C math library), and in particular random number generation
algorithms. Example code is:
....
gsl_rng * r;
....
double u = gsl_rng_uniform (r);
The problem is that r "represent" the iteration of the random number
generator algorithm, so it needs to be accessible every time you need
to generate a new random number. In C you can solve the problem by:
1) passing r to every function that needs to generate random numbers
(a burden....)
2) define r as global variable and use extern in evey file (not very
elegant)
I'm wondering if there is a better way to solve the problem in c++. I
also would like to toggle any explicit dependence in my code to the
gsl. This prompted the creation of a wrapper class like:
class RandomNumberGenerator
{
private:
gsl_rng* r;
public:
RandomNumberGenerator() ;
double generateUniform();
};
Then I would like to have something like a unique object of this class
available everywhere that does not need to be initilized (static?)
Is it possible to accomplish this task?
Do you have some better solutions to propose?
Thank you in advance for any help.
StephQ
(it is a C math library), and in particular random number generation
algorithms. Example code is:
....
gsl_rng * r;
....
double u = gsl_rng_uniform (r);
The problem is that r "represent" the iteration of the random number
generator algorithm, so it needs to be accessible every time you need
to generate a new random number. In C you can solve the problem by:
1) passing r to every function that needs to generate random numbers
(a burden....)
2) define r as global variable and use extern in evey file (not very
elegant)
I'm wondering if there is a better way to solve the problem in c++. I
also would like to toggle any explicit dependence in my code to the
gsl. This prompted the creation of a wrapper class like:
class RandomNumberGenerator
{
private:
gsl_rng* r;
public:
RandomNumberGenerator() ;
double generateUniform();
};
Then I would like to have something like a unique object of this class
available everywhere that does not need to be initilized (static?)
Is it possible to accomplish this task?
Do you have some better solutions to propose?
Thank you in advance for any help.
StephQ