I have been playing with Boost.Random for a day now, and while boost::uniform_int_distribution<> works well, I am having trouble with boost::exponential_distribution<>.
A simple program is worth a thousand words:
#include <iostream>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/exponential_distribution.hpp>
int main() {
boost::mt19937 gen;
boost::exponential_distribution<> dis;
std::cout << dis(gen) << "\n";
return 0;
}
Compiled with Clang 3.0, using Boost 1.39.1 (no, I cannot upgrade Boost).
The output is invariably the same: nan.
I could not find any reported issue so I guess it’s me not using the library correctly… Any clue would be appreciated.
Given a random number
r, uniformly distributed on [0,1),-log(r)is distributed on (0,infinity) with the distributionexp(-x).The former is
boost::uniform_01().If you need a distribution
p exp(-px), then it’s-(1/p)log(r).In both cases here
log(x)is a natural log (basee).UPD: Using
boost::variate_generatorseems to work for me (boost 1.43):I did not check the distribution though.