为什么升压uniform_int_distribution需要一个封闭的范围内(而不是半开区间,以下常见的C ++使用)? [英] Why boost uniform_int_distribution takes a closed range (instead of a half-open range, following common C++ usage)?

查看:381
本文介绍了为什么升压uniform_int_distribution需要一个封闭的范围内(而不是半开区间,以下常见的C ++使用)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了一切。甚至还有href=\"http://www.boost.org/doc/libs/1_55_0/doc/html/boost_random/tutorial.html\" rel=\"nofollow\">在文档页面警告一个

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

为什么这样做时,在C ++中常见的做法就是用开放的范围[开始,结束)?

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

推荐答案

只有在封闭的范围内,你可以创建一个 uniform_int_distribution ,产生任何整数:

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

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

如果这将是一个半开放的范围内,你永远无法达到的std :: numeric_limits&LT; INT&GT; :: MAX(),但只有的std :: numeric_limits&LT; INT方式&gt; :: MAX() - 1

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

这对的std ::在C ++ 11标准库uniform_int_distribution 同样的情况。

有关迭代半开的范围很常见,因为人们可以很容易的前preSS空范围(通过设置开始==结束)。这是没有意义的分布。

Half-open ranges for iterators are common, because one can easily express empty ranges (by setting begin == end). This doesn't make sense for distributions.

参考:斯蒂芬T. Lavavej提到了他的谈话 RAND()在融入本土2013(约14分钟)有害。这次演讲是关于C ++ 11 &LT;随机方式&gt; ,当然,同样的道理也适用于提升以及

Reference: Stephan T. Lavavej mentions this exact reason in his talk "rand() Considered Harmful" at Going Native 2013 (around minute 14). This talk is about C++11 <random>, but of course the same reasoning applies to boost as well.

这篇关于为什么升压uniform_int_distribution需要一个封闭的范围内(而不是半开区间,以下常见的C ++使用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆