std::generate_canonical (3) - Linux Manuals
std::generate_canonical: std::generate_canonical
NAME
std::generate_canonical - std::generate_canonical
Synopsis
Defined in header <random>
template< class RealType, size_t bits, class Generator > (since C++11)
RealType generate_canonical( Generator& g );
Generates a random floating point number in range [0, 1).
To generate enough entropy, generate_canonical() will call g() exactly k times, where \(k = max(1, \lceil \frac{b}{log_2 R} \rceil)\)k = max(1, ⌈ b / log
2 R ⌉) and
* b = std::min<std::size_t>(bits, std::numeric_limits<RealType>::digits)
* R = g.max() - g.min() + 1.
Parameters
g - generator to use to acquire entropy
Return value
Floating point value in range [0, 1).
Exceptions
None except from those thrown by g
Notes
Some existing implementations have a bug where they may occasionally return 1.0 if RealType is float GCC_#63176 LLVM_#18767. This is LWG_issue_2524
Example
produce random numbers with 10 bits of randomness: this may produce only k*R distinct values
// Run this code
Possible output:
See also
uniform_real_distribution produces real values evenly distributed across a range
(C++11)