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.
3
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.
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].
4