This is probably a silly question, but I can't see what I am doing wrong here. I have the class:
#include <sys/time.h>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_randist.h>
#include <cmath>
#include "randomnumbergenerator.h"
class RandomNumberGenerator
{
gsl_rng * rn;
public:
RandomNumberGenerator();
~RandomNumberGenerator();
double univariate();
void bivariateGaussian(double rho, double &x, double &y);
};
long currentMicroseconds()
{
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_usec;
}
RandomNumberGenerator::RandomNumberGenerator()
{
const gsl_rng_type * T;
gsl_rng_env_setup();
//T = gsl_rng_default;
T = gsl_rng_mt19937;
rn = gsl_rng_alloc (T);
gsl_rng_set(rn,currentMicroseconds());
}
double RandomNumberGenerator::univariate()
{
return gsl_rng_uniform(rn);
}
void RandomNumberGenerator::bivariateGaussian(double rho, double &x, double &y)
{
gsl_ran_bivariate_gaussian (rn, 1.0, 1.0, rho, &x, &y);
}
RandomNumberGenerator::~RandomNumberGenerator()
{
gsl_rng_free (rn);
}
Which I call from here:
double x;
double y;
rng.bivariateGaussian(rho, x, y);
but I get a segmentation fault on gsl_ran_bivariate_gaussian (rn, 1.0, 1.0, rho, &x, &y);
Any idea?
check to see if rn
has really been allocated. It is probably the only thing that can cause segmentation fault.
i tested your code on my computer, it runs okay as far as they can tell. May be check installation of GSL, they have a test suite you can use
Which compiler? I assume that rn
is a member variable of RandomNumberGenerator. Do you initialize it to 0 in the constructor? You don't seem to be checking for an error return from gsl_rng_alloc, you probably should be because the only thing I can see right off that may be causing a problem is if rn
isn't pointing to anything valid at the call that's segfaulting.
Looking at the manual for gsl_rng_alloc
you can check to see if it returns NULL
or 0 and then throw an exception if it doesn't. For example:
#include <stdexcept>
RandomNumberGenerator::RandomNumberGenerator()
{
const gsl_rng_type * T;
gsl_rng_env_setup();
//T = gsl_rng_default;
T = gsl_rng_mt19937;
rn = gsl_rng_alloc (T);
if (rn == 0) {
throw ::std::runtime_error("Failed to allocation a random number generator.");
}
gsl_rng_set(rn,currentMicroseconds());
}
Also, have you tried compiling with -O0
to turn of all optimization?
In:
double x;
double y;
rng.bivariateGaussian(rho, x, y);
are x and y perhaps supposed to be arrays rather than single variables? I'd expect a distribution to produce N values rather than one (or two).
I don't program c++, but C. Hopefully this will someway also apply to you. But on CI sometimes use a debugger like GDB or the debugger in Eclipse. I also use valgrind (I really like this tool a lot) to fix memory leaks/segmentation faults. I advice you to like at this tutorial to get a better understanding of what valgrind can do for you. Valgrind can do a lot more so I would advice you to read about valgrind/helgrind.
thanks everyone for your answers. The bug was in the piece of code that I didn't paste :( I was passing an instance of RandomNumberGenerator as a normal parameter. When I changed it to passing as reference it started to work magically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.