Why does boost uniform_int_distribution accept a closed range (instead of a half-open range, after normal use in C ++)?

The title says it all. There's even a warning on the documentation pages:

enter image description here

Why do this when the usual practice in C ++ is to use open ranges [begin, end)?

+3
source share
1 answer

Only with closed ranges can you create a uniform_int_distribution that produces any integer:

 uniform_int_distribution<int> dist(std::numeric_limits<int>::min(), std::numeric_limits<int>::max()); 

If this is a half-open range, you will never reach std::numeric_limits<int>::max() , but only std::numeric_limits<int>::max() - 1 .

This is the same situation for std::uniform_int_distribution in the C ++ 11 standard library.

Half-open ranges for iterators are common because empty ranges can be easily expressed (by setting begin == end ). This does not make sense for distributions.


Ref: Stefan T. Loveaway mentions this exact reason in his conversation “rand () is considered harmful” in Going Native 2013 (about 14 minutes). This talk is about C ++ 11 <random> , but, of course, the same reasoning applies to boost as well.

+5
source

Source: https://habr.com/ru/post/1245036/


All Articles