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 RandomNumberGen erator
{
private:
gsl_rng* r;
public:
RandomNumberGen erator() ;
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 18 7855
"StephQ" <as*******@mail inator.comwrote in message
news:11******** **************@ s33g2000prh.goo glegroups.com.. .
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)
How about defining r as a global variable, then declaring it as extern in
just *one* header file, and simply including that header in any
implementation file which needs to use r?
(Bad name, "r", though. Something more descriptive of its purpose, and less
likely to have a name clash with local variables, would be better.)
-Howard
On Thu, 26 Apr 2007 18:11:54 GMT, "Howard" wrote:
>"StephQ" wrote:
>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)
How about defining r as a global variable, then declaring it as extern in just *one* header file, and simply including that header in any implementati on file which needs to use r?
Non-const global variables have severe drawbacks and are usually not
considered good style. Since the variable is needed to '"represent"
the iteration of the random number generator algorithm' I don't think
it's a 'burden' to pass it to functions.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Roland Pibinger wrote:
On Thu, 26 Apr 2007 18:11:54 GMT, "Howard" wrote:
>"StephQ" wrote:
>>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)
How about defining r as a global variable, then declaring it as extern in just *one* header file, and simply including that header in any implementati on file which needs to use r?
Non-const global variables have severe drawbacks and are usually not
considered good style. Since the variable is needed to '"represent"
the iteration of the random number generator algorithm' I don't think
it's a 'burden' to pass it to functions.
I second that.
For classes at the "applicatio n" level, I usually practice passing
around an "environmen t" class that contains resources such as a
preferences manager, logging manager and anything else like this.
In the Austria C++ alpha ( http://netcabletv.org/public_releases/) there
is an Environment (at_env.h) template which does just that.
On 4/26/07 11:03 AM, in article 11************* *********@s33g2 00...legr oups.com, "StephQ"
<as*******@mail inator.comwrote :
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)
Why not implement a function that wraps the library routine:
double GenerateRandomN umber()
{
static gsl_rng * r = NULL;
return gsl_rng_uniform ( r);
}
Greg
"StephQ" <as*******@mail inator.comwrote in message
news:11******** **************@ s33g2000prh.goo glegroups.com.. .
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 RandomNumberGen erator
{
private:
gsl_rng* r;
public:
RandomNumberGen erator() ;
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.
class RandomNumberGen erator
{
public:
double GenerateUniform () { return gsl_rng_uniform (&Seed) };
private:
static gsl_rng Seed;
};
gsl_rng RandomNumberGen erator::Seed = 0;
Notice, Seed is declared as an instance, not as a pointer. gsl_rng_uniform
needs the address of the variable which is accomplished by passing the
address of seed. In your sample you never allocated storage for r.
"Greg Herlihy" <gr****@pacbell .netwrote in message
news:C2******** **********@pacb ell.net...
>
On 4/26/07 11:03 AM, in article 11************* *********@s33g2 00...legr oups.com, "StephQ"
<as*******@mail inator.comwrote :
>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)
Why not implement a function that wraps the library routine:
double GenerateRandomN umber()
{
static gsl_rng * r = NULL;
return gsl_rng_uniform ( r);
}
Yes, this would work better than a class, IMO. Just have a static variable
in a function. Of course it should probably be:
double GenerateRandomN umber()
{
static gsl_rng r = 0;
return gsl_rng_uniform (&r);
}
or whatever you initialize gsl_rng with.
Jim Langston wrote:
"StephQ" <as*******@mail inator.comwrote in message
news:11******** **************@ s33g2000prh.goo glegroups.com.. .
>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 RandomNumberGen erator { private: gsl_rng* r;
public:
RandomNumberGen erator() ;
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.
class RandomNumberGen erator
{
public:
double GenerateUniform () { return gsl_rng_uniform (&Seed) };
Would it be better to make this static as well? Then you wouldn't have
to create objects of this class.
private:
static gsl_rng Seed;
};
gsl_rng RandomNumberGen erator::Seed = 0;
Notice, Seed is declared as an instance, not as a pointer. gsl_rng_uniform
needs the address of the variable which is accomplished by passing the
address of seed. In your sample you never allocated storage for r.
class RandomNumberGen erator
{
public:
double GenerateUniform () { return gsl_rng_uniform (&Seed) };
Would it be better to make this static as well? Then you wouldn't have
to create objects of this class.
private:
static gsl_rng Seed;
};
gsl_rng RandomNumberGen erator::Seed = 0;
Do you see any evident problem in the the following solution?
class RandomNumber
{
private:
const gsl_rng_type* T;
gsl_rng* r;
public:
RandomNumber() ;
// Uniform random number generator.
double generateUniform ();
// Distributions random number generators.
double generateGaussia n(double mu, double sigma);
......
};
static RandomNumber randomNumber;
I would like to avoid passing r around because:
1) it's related to the use of the gsl library. And I would like to be
felixible about the library used for random number generation.
2) in stochastic simulations too many algorithms depend on the
generations of random numbers. I would really have to pass r even to
class constructors and so on.....
p.s. I know it is not a good name, r. It was just an example from the
gsl documentation :)
Thank you
StephQ
StephQ wrote:
>>class RandomNumberGen erator { public: double GenerateUniform () { return gsl_rng_uniform (&Seed) };
Would it be better to make this static as well? Then you wouldn't have to create objects of this class.
>>private: static gsl_rng Seed; }; gsl_rng RandomNumberGen erator::Seed = 0;
Do you see any evident problem in the the following solution?
class RandomNumber
{
private:
const gsl_rng_type* T;
gsl_rng* r;
public:
RandomNumber() ;
// Uniform random number generator.
double generateUniform ();
// Distributions random number generators.
double generateGaussia n(double mu, double sigma);
......
};
static RandomNumber randomNumber;
I would like to avoid passing r around because:
1) it's related to the use of the gsl library. And I would like to be
felixible about the library used for random number generation.
2) in stochastic simulations too many algorithms depend on the
generations of random numbers. I would really have to pass r even to
class constructors and so on.....
So, you want to use the solution from Greg Herlihy:
double GenerateRandomN umber()
{
static gsl_rng * r = NULL;
return gsl_rng_uniform ( r);
}
Or you can put this function to a class, and declare it static, you will
not have to pass your r to constructor:
/// in the header file
class RandomNumber
{
public:
static double generate();
}
/// in the cpp file
double RandomNumber::g enerate()
{
static gsl_rng * r = NULL;
return gsl_rng_uniform ( r );
}
You can call it like:
RandomNumber::g enerate()
from any file including RandomNumber header file This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: A |
last post by:
Hi,
Consider this code:
//Header File - Foo.h
int i = 0; // non-static global variable
class Foo{
...
|
by: jose luis fernandez diaz |
last post by:
Hi,
I am reading Stroustrup's book 'C++ Programming Language'. In the
10.4.9 section (Nonlocal Store) he says:
"A variable defined outside any function (that is global, namespace,
and class static variables) is initializated (constructed) before main
is invoked . . ."
.. . .
|
by: Kleenex |
last post by:
Reason: I am working on an embedded project which has very limited
memory (under 512 bytes, 60 or so of which is stack space), which
translates into limited stack space. In order to save on stack space, I
tried to only use parameters and stack space for things which are truely
temporary. Instead of passing a pointer to a data structure which should
always be populated with data, I have the data structure declared as a
global variable and...
|
by: Michael |
last post by:
Hi newsgroup,
as the subject indicates I am looking for an advice
using global variables. I am not if this problem is more
about style then C. If its wrong in thi group, sorry.
So I have a couple of function that all need the same
information (all located in the same file). By now it looks like
/* file beginns */
|
by: fabio |
last post by:
Why? i' ve heard about this, the usage of global vars instead of
locals is discouraged, but why?
thx :)
| |
by: zeecanvas |
last post by:
Hi,
First of all: Yes, I know global variables are bad, but I've a huge
amount of legacy code, and I've to maintain it _as_is_.
I'm maintaining a big program. I moved all (program-wide scope) global
variables outside of the files they were defined it, and created some
files that just hold global variables definitions (just variables,
without any function definition). So, depending on the purpose/category
of variables, they're defined...
|
by: Jay Wolfe |
last post by:
Hello,
I'm trying to make sure I use best practices (and hence save myself some
headaches) with the declaration and definition of global variables. Let's
say I have an app with 30 files, including main.cpp I have global
variables that need defining in main.cpp and declaring in all other files
in. The way I've seen it done is to define/declare everything in one header
file (e.g. globalincludes.h) prefaced with the word EXTERN
...
|
by: eoindeb |
last post by:
Sorry to ask another global variable question, but from reading other
posts I'm still not sure whether to use them or not.
I have a program with a set function that calls 4 other functions in
order - let's say function A, B, C, D.
It always calls function A first which is a function that returns a
system path. Now all other functions require that variable as well
(function A returns a char pointer)
|
by: Jaco Naude |
last post by:
Hi,
I'm using a static library in my application which links fine except
for a few global variables. The static library only contains a bunch
of .cpp and .h files and the global variables are defined as follows:
extern unsigned mgl_numg, mgl_cur;
extern float mgl_fact;
extern long mgl_gen_fnt;
extern short mgl_buf_fnt;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |