std::uniform_real_ distribution class in C++ with Examples

In Probability, Uniform Distribution Function refers to the distribution in which the probabilities are defined on a continuous random variable, one which can take any value between two numbers, then the distribution is said to be a continuous probability distribution. For example, the temperature throughout a given day can be represented by a continuous random variable and the corresponding probability distribution is said to be continuous.

     \[ f(x) =  \frac{1}{b-a}, & a\leq x < b\\ \]

C++ have introduced uniform_real_distribution class in the random library whose member function give random real numbers or continuous values from a given input range with uniform probabilty.



Public member functions in uniform_real_distribution class:

  1. operator(): This function returns a random value from the range given. The datatype of the return value is specified during initialization of the template class. The probability for any value is same. The time complexity for this operation is O(1).

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ code to demonstrate the working of
    // operator() function
      
    #include <iostream>
      
    // for uniform_real_distribution function
    #include <random>
      
    using namespace std;
      
    int main()
    {
        // Here default_random_engine object
        // is used as source of randomness
        // We can give seed also to default_random_engine
        // if psuedorandom numbers are required
        default_random_engine generator;
      
        double a = 0.0, b = 1.0;
      
        // Initializing of uniform_real_distribution class
        uniform_real_distribution<double> distribution(a, b);
      
        // number of experiments
        const int num_of_exp = 10000000;
        // number of ranges
        int n = 100;
        int p[n] = {};
        for (int i = 0; i < num_of_exp; ++i) {
      
            // using operator() function
            // to give random values
            double number = distribution(generator);
            ++p[int(number * n)];
        }
      
        cout << "Probability of some ranges" << endl;
        // Displaying the probability of some ranges
        // after generating values 10000 times.
        cout << "0.50-0.51"
             << " " << (float)p[50] / (float)num_of_exp << endl;
        cout << "0.60-0.61"
             << " " << (float)p[60] / (float)num_of_exp << endl;
        cout << "0.45-0.46"
             << " " << (float)p[45] / (float)num_of_exp << endl;
        return 0;
    }

    chevron_right

    
    

    Output:

    Probability of some ranges
    0.50-0.51 0.0099808
    0.60-0.61 0.0099719
    0.45-0.46 0.009999
    

    The probability of all ranges are almost equal.

  2. The other member functions are:

  3. a(): Returns lower bound of range.
  4. b(): Returns upper bound of range.
  5. min(): Returns minimum value the function can return. For uniform distribution min() and a() return same value.
  6. max(): Returns minimum value the function can return. For uniform distribution min() and a() return same value.
  7. reset(): This function resets the distribution such that the next random values generated are not based on the previous values.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to demonstrate the working of
// a(), b(), min(), max(), reset() function
  
#include <iostream>
  
// for uniform_real_distribution function
#include <random>
  
using namespace std;
  
int main()
{
    double a = 0, b = 1.5;
  
    // Initializing of uniform_real_distribution class
    uniform_real_distribution<double> distribution(a, b);
  
    // Using a() and b()
    cout << "Lower Bound"
         << " " << distribution.a() << endl;
    cout << "Upper Bound"
         << " " << distribution.b() << endl;
  
    // Using min() and max()
    cout << "Minimum possible output"
         << " " << distribution.min() << endl;
    cout << "Maximum possible output"
         << " " << distribution.max() << endl;
  
    // Using reset function
    distribution.reset();
  
    return 0;
}

chevron_right


Output:

Lower Bound 0
Upper Bound 1.5
Minimum possible output 0
Maximum possible output 1.5

Reference:
http://www.cplusplus.com/reference/random/uniform_real_distribution/



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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



Improved By : nidhi_biet



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.