jeudi 7 juillet 2016

Bus Error: 10 when generating random numbers

I am having a really odd issue with a numerical simulation that I have created. It is essentially a Monte Carlo simulation that generates a series of random numbers from a few different distributions. One of those distributions is an exponential distribution. In order to generate a random number from this distribution, I use the following function:

long expd(double mean){
    //Exponential random number generator

    return -(long)round(mean*log( (double)rand()/RAND_MAX) );
}

The strange thing is, that this function works 99.99% of the time. But, every few ten thousandths of a call, I get "Bus Error: 10". However, if I remove the RAND_MAX and just call this:

long expd(double mean){
    //Exponential random number generator

    return -(long)round(mean*log( (double)rand() );
}

it executes with no problems (but obviously I cannot use this).

I have searched everywhere, and I cannot figure out what is going on. I know that a bus error occurs when the memory being addressed is not accessible/existent, but I just don't see how using a simple macro like RAND_MAX could cause this error to be thrown.

Does anyone have any idea why this would happen? Thanks.




Aucun commentaire:

Enregistrer un commentaire