2

In the c++11, we now have <random> to produce random number. About uniform distributions, we have following int_distribution and double_distribution:

uniform_int_distribution-produces integer values evenly distributed across a range

uniform_real_distribution-produces real values evenly distributed across a range

The first produces random integer values i, uniformly distributed on the closed interval [a, b], but the second, ie real one, produces random floating-point values i, uniformly distributed on the interval [a, b)

Question: why int and real distribution have different range, ie for one b] is inclusive but for another b) is exclusive?

If it is related with the algorithms behind, I would like to know what are they and where are the differences. Or preferences about it.

  • Related reading: Random in C++11 with closed interval. – user22815 Feb 19 '15 at 13:48
  • I cannot answer definitely, but consider the range 0..1. With integers, an open range would always return 0. With floating point, it cannot return 1, but there are a very large number of values it can return. If you need different behavior, you are free to define your own distribution object. – user22815 Feb 19 '15 at 13:55
  • For int distributions there's two use cases: either you care about the number of values but not the range (e.g. for simulating probabilistic events) or you do care about the range. For the former an open interval is more useful because it's easier to calculate the number of values (max - min). For the latter case you already know the min and max you want to get so the inclusive format is easier. It seems they're catering to the latter use case. I'm not sure what's the rationale behind excluding the maximum in the real distribution though. – Doval Feb 19 '15 at 14:43
2

Uniform_real_distribution had a closed range until 2006, after which it was changed to a half-open range because developers are said to be more comfortable with it.

1

Closed intervals "feel" more natural in a discrete setting. Half-open intervals "feel" more natural in a continuous setting (well, they feel more natural even for rationals, but...).

I would be surprised by a "generates integer numbers" function that didn't use intervals if I gave it an explicit minimum and maximum, but I'm essentially OK with a single-argument one that takes rand(N) and gives me a result in [0, N-1], but if I give it rand(a, b) I actually want [a, b].

  • Question asks for a reason, a "feeling" is not a reason, at least not when defining a standard for a programming library. – user22815 Feb 19 '15 at 13:52
  • 2
    Half-open intervals are very natural in discrete settings. For example, array indexing and slicing, or range in Python, or any other interval of the form start <= x < end. See also: Dijkstra's take on it. – user7043 Feb 19 '15 at 14:40
  • I think open intervals make more sense in the continuous setting in statistics since the probability of any one point is zero the difference melts away. However, since we are programmers and actually mean a fine grained discrete settings, yeah maybe half open is more natural, don't really know. Just my tuppence worth. – Nathan Cooper Feb 19 '15 at 17:43
  • 1
    @delnan Fun fact: closed intervals are more natural with 1-based arrays. – immibis Feb 20 '15 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.