GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::generate_canonical(3) C++ Standard Libary std::generate_canonical(3)

std::generate_canonical - std::generate_canonical


Defined in header <random>
template< class RealType, std::size_t Bits, class Generator > (since C++11)
RealType generate_canonical( Generator& g );


Generates a random floating point number in range \(\small [0,1)\)[0, 1).


To generate enough entropy, generate_canonical() will call g() exactly \(\small k\)k
times, where \(\small k = \max(1, \lceil \frac{b}{\log_2 R} \rceil)\)k = max(1, ⌈
b / log
2 R ⌉) and


* b = std::min(Bits, std::size_t{std::numeric_limits<RealType>::digits}),
* R = g.max() - g.min() + 1.


g - generator to use to acquire entropy


Floating point value in range \(\small [0,1)\)[0, 1).


None except from those thrown by g


Some existing implementations have a bug where they may occasionally return 1.0 if
RealType is float GCC #63176 LLVM #18767 MSVC STL #1074. This is LWG issue 2524.


produce random numbers with 10 bits of randomness: this may produce only k*R
distinct values

// Run this code


#include <random>
#include <iostream>


int main()
{
std::random_device rd;
std::mt19937 gen(rd());
for(int n=0; n<10; ++n) {
std::cout << std::generate_canonical<double, 10>(gen) << ' ';
}
}


0.208143 0.824147 0.0278604 0.343183 0.0173263 0.864057 0.647037 0.539467 0.0583497 0.609219


uniform_real_distribution produces real values evenly distributed across a range
(C++11) (class template)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.