[Random]Random value from range

Hello, After experiments I wrote below function. Could you verify if function is written correctly for random value from range [min,max) and uniform distribution? double RandomGenerator(const double min, const double max) { assert(min<max); static boost::lagged_fibonacci44497 gen(static_cast<unsigned int>(std::time(0))); static boost::uniform_real<> range(min, max); static boost::variate_generator<boost::lagged_fibonacci44497&, boost::uniform_real<> > generator(gen, range); const double value = generator(); return value; } I have few question related to random library: 1. Is it possibility to write function which will generate numbers from range [min, max] or (min, max) 2. Why generator (boost::lagged_fibonacci44497)is not properly initialized in some default constructor. Without static_cast<unsigned int>(std::time(0)) code generated the same values after every application run. 3. Why when I missed reference "&" in line boost::variate_generator<boost::lagged_fibonacci44497&, application crashes. Is it bug? Best regards, Mariusz

AMDG Mariusz Kwiczala wrote:
After experiments I wrote below function. Could you verify if function is written correctly for random value from range [min,max) and uniform distribution?
double RandomGenerator(const double min, const double max) { assert(min<max); static boost::lagged_fibonacci44497 gen(static_cast<unsigned int>(std::time(0))); static boost::uniform_real<> range(min, max); static boost::variate_generator<boost::lagged_fibonacci44497&, boost::uniform_real<> > generator(gen, range); const double value = generator(); return value; }
I have few question related to random library: 1. Is it possibility to write function which will generate numbers from range [min, max] or (min, max)
(min, max) can be handled by rejection from [min, max). [min, max] is a bit trickier. Either way, you'll have to implement it yourself.
2. Why generator (boost::lagged_fibonacci44497)is not properly initialized in some default constructor. Without static_cast<unsigned int>(std::time(0)) code generated the same values after every application run.
This is a feature.
3. Why when I missed reference "&" in line boost::variate_generator<boost::lagged_fibonacci44497&, application crashes.
Probably stack overflow. boost::lagged_fibonacci44497 is rather large.
Is it bug?
In Christ, Steven Watanabe

On Jan 3, 2010, at 1:59 PM, Steven Watanabe wrote:
I have few question related to random library: 1. Is it possibility to write function which will generate numbers from range [min, max] or (min, max)
(min, max) can be handled by rejection from [min, max). [min, max] is a bit trickier. Either way, you'll have to implement it yourself.
[min, max] isn't that much trickier. It can be handled by rejection from [min, max+ε). In fact, for floating types, the float_next function from the boost math toolkit will provide an ε that doesn't require rejection. The float_prior function allows (min, max) to be done without rejection. Equivalent things can be done for any numeric type with finite precision (including discrete numeric types). Indefinite precision, though, will generally require rejection. An ε very much smaller than max-min will rarely take the rejection path. Topher Cooper
participants (3)
-
Mariusz Kwiczala
-
Steven Watanabe
-
Topher Cooper