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
rnhas 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