Opened 8 years ago

Last modified 8 years ago

#10064 new Bugs

generate_uniform_real may go into infinite loop

Reported by: Markus Faerber <mf@…> Owned by: No-Maintainer
Milestone: To Be Determined Component: random
Version: Boost 1.54.0 Severity: Problem
Keywords: Cc:

Description

See uniform_real_distribution.hpp:

template<class Engine, class T> T generate_uniform_real(

Engine& eng, T min_value, T max_value, boost::mpl::true_ / is_integral<Engine::result_type> */)

{

for(;;) {

typedef T result_type; typedef typename Engine::result_type base_result; result_type numerator = static_cast<T>(subtract<base_result>()(eng(), (eng.min)())); result_type divisor = static_cast<T>(subtract<base_result>()((eng.max)(), (eng.min)())) + 1; BOOST_ASSERT(divisor > 0); BOOST_ASSERT(numerator >= 0 && numerator <= divisor); T result = numerator / divisor * (max_value - min_value) + min_value; if(result < max_value) return result;

}

}

Please add a "BOOST_ASSERT(max_value > min_value);".

If one declares something like

boost::random::mt19937 mt; boost::random::uniform_real_distribution<double> dist(0.0, 0.0);

dist(mt) makes generate_uniform_real go into an infinite loop.

Thank you and best regards.

Attachments (1)

comments_patch.diff (852 bytes ) - added by harris.pc@… 8 years ago.

Download all attachments as: .zip

Change History (2)

by harris.pc@…, 8 years ago

Attachment: comments_patch.diff added

comment:1 by harris.pc@…, 8 years ago

Hi,

I see you have added the asserts, but could you also please adjust the comments? see attached patch,

Thanks, Paul

Note: See TracTickets for help on using tickets.