generate_canonical Function template in C++

This template in C++ is used to convert the value generated by g into a floating point value in the range [0, 1) preserving the uniformity properties of sequences generated with g. To generate enough entropy generate_canonical() will call g() exactly ‘k’ times where,

 k = max(1, \lceil \frac{b}{log_2 R} \rceil)


template ( class RealType, size_t bits, class URNG )
RealType generate_canonical (URNG& g);

Template parameters: This template accepts three parameters as mentioned above and described below:

  • RealType: The function returns a value of floating point type.
  • bits: Maximum number of bits in the mantissa.
  • URNG: A uniform random number generator class.

Parameters: The generate_canonical() function accepts single parameter g which is used as uniform random number generator object. It is used to acquire entropy.

Return value: This function returns a floating point value in range [0, 1).

Below programs illustrate the above C++ template





// C++ program to illustrate generate_canonical()
// function.
#include <bits/stdc++.h>
using namespace std;
int main()
    // Obtain a root from the system clock:
    unsigned root = chrono::system_clock::now().time_since_epoch().count();
    // Random number engine class
    // that generates pseudo-random numbers
    default_random_engine generator(root);
    double can_val = generate_canonical<double
    // Print the random canonical value
    // It will display different value everytime
    cout << "Random canonical value: " << can_val;
    return 0;



Random canonical value: 0.0281975


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.