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.
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.max - min
). For the latter case you already know themin
andmax
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.