In a project, I am generating millions of expo(lambda) random variables, where lambda is potentially very large. When using std::exponential_distribution<float>
I occasionally get a return value of inf
. I would understand this if lambda were close to 0, but when lambda is large a value very close to zero is expected. For example, the following program usually terminates after several million draws:
#include<iostream>
#include<random>
int main(void)
{
std::random_device rd;
std::mt19937 generator(rd());
float lambda = 1000000.0;
float val = 0;
for(int i = 0; i < 1000000000; ++i)
{
std::exponential_distribution<float> dist(lambda);
val = dist(generator);
if(isinf(val))
{
std::cout << i << " " << val << " failure" << std::endl;
exit(0);
}
}
return 0;
}
If there is some error (due to precision) in the function, why does it return inf
instead of the more convenient 0.0
? Is there any way to fix this, besides for manually checking that the output is finite? Thanks.
Aucun commentaire:
Enregistrer un commentaire