Boost logo

Boost Users :

Subject: [Boost-users] [random] infinite loop in uniform_real_distribution
From: Jérémy Coulon (jeremy.coulon_at_[hidden])
Date: 2013-11-25 14:16:40


Hello,

I'm using Boost 1.54.
The following code hangs in infinite loop when generating a single
floating point number :

#include <boost/random.hpp>
#include <iostream>
#include <limits>

int main()
{
     boost::random::mt19937 rng;
     boost::random::uniform_real_distribution<float> distrib(
                 -std::numeric_limits<float>::max(),
                 std::numeric_limits<float>::max());

     std::cout << distrib(rng) << std::endl;
     return 0;
}

I can reproduce the bug with both
- Linux GCC 4.8 x86_64 and
- Windows VisualStudio 2012 32bits

It looks like this uniform distribution doesn't work if the range
[min,max] is large.
 From uniform_real_distribution.hpp:61
T result = numerator / divisor * (max_value - min_value) + min_value;

Here the operation (max_value - min_value) produces an overflow.

Regards,
Jeremy


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net