Question

I have created a simulation environment which has several stochastic parts involved. I draw numbers from normal, uniform and lognormal distributions. In most of the cases this runs fine, however, when I decide to do 100 simulations after each other I am getting the error: R6010 Abort() has been called. In my console I get the error: invalid argument for mersenne_twister::seed. However, I am only using the standard pseudo-random number generator rand(). At no point I call mersene_twister. So this probably is a method from the std::normal_distribution.

Furthermore I don't seed why my seed value is invalid after X iterations and not for the first X iterations?

Does anyone have any experience with this error? Does anyone have any suggestions how to solve this

P.s. srand(time(0)) is called only once, in the beginning of the main. While all random numbers are generated in a second class "random_num". P.s.s I am aware that this might not be the best way to generate random numbers, however it is sufficient for my purpose.

The code as requested for the RNG:

double random_num::uniform(int lb, int ub)//Generate uniformly distributed random numbers with lowerbound lb and upperbound ub
{
    //srand(time(0));
    double number;
    number =((double) rand() / (RAND_MAX+1)) * (ub-lb+1) + lb;

    return number;
}

double random_num::normal(double mean,  double var) //Generate normally distributed random numbers with mean and variance
{

    //srand(time(0));
    default_random_engine generator (rand());

    normal_distribution<double> distribution(mean, var);

    return distribution(generator);
}

double random_num::lognormal(double mean, double var, double offset)
{
    //srand(time(0));
    random_num dummy;
    double random;


    random = exp(dummy.normal(mean,var))-offset; //Calculate the 3 parameter lognormal

    return random;
}
Was it helpful?

Solution

@lip The problem was indeed that rand() returned a zero at some moment. And therefore default_random_engine generator(0); aborted.

The solution was quite simple: Create a function that checks that rand() it is not a zero:

int rand0()
{
   int dummy = rand();
   while(dummy==0)
   {
     dummy = rand();
   }
   return dummy;
}

And then: default_random_engine generator(rand0());

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top