std::uniform_real_distribution
Header: <random>
Produces random floating-point values (\small x)x, uniformly distributed on the interval (\small [a, b))[a, b), that is, distributed according to the probability density function:
# Declarations
template< class RealType = double >
class uniform_real_distribution;
(since C++11)
# Notes
It is difficult to create a distribution over the closed interval (\small[a, b])[a, b] from this distribution. Using std::nextafter(b, std::numeric_limits
Most existing implementations have a bug where they may occasionally return (\small b)b (GCC #63176 LLVM #18767 MSVC STL #1074). This was originally only thought to happen when RealType is float and when LWG issue 2524 is present, but it has since been shown that neither is required to trigger the bug.
# Example
#include <iostream>
#include <random>
int main()
{
std::random_device rd; // Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<> dis(1.0, 2.0);
for (int n = 0; n < 10; ++n)
// Use dis to transform the random unsigned int generated by gen into a
// double in [1, 2). Each call to dis(gen) generates a new random double.
std::cout << dis(gen) << ' ';
std::cout << '\n';
}