std::uniform_int_distribution class in C++


In Probability, Discrete Uniform Distribution Function refers to the distribution with constant probability for discrete values over a range and zero probability outside the range. The probability density function P(x) for uniform discrete distribution in interval [a, b] is constant for discrete values in the range [a, b] and zero otherwise. Mathematically the function is defined as:

     \[ f(x) =  \begin{cases} \frac{1}{b-a}, & a\leq x \leq b\\ 0, & \text{otherwise}\\ \end{cases} \]

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

Public member functions in uniform_int_distribution class:

  1. operator(): This function returns a random number from the given range of distribution. The probability for any number to be obtained from this function is same. Operator() function takes constant time for generation.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ code to demonstrate the working of
    // operator() function
      
    #include <iostream>
      
    // for uniform_int_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;
      
        int a = 0, b = 9;
      
        // Initializing of uniform_int_distribution class
        uniform_int_distribution<int> distribution(a, b);
      
        // number of experiments
        const int num_of_exp = 10000;
      
        int n = b - a + 1;
        int p[n] = {};
        for (int i = 0; i < num_of_exp; ++i) {
      
            // using operator() function
            // to give random values
            int number = distribution(generator);
            ++p[number];
        }
      
        cout << "Expected probability: "
             << float(1) / float(n) << endl;
      
        cout << "uniform_int_distribution ("
             << a << ", " << b << ")" << endl;
      
        // Displaying the probability of each number
        // after generating values 10000 times.
        for (int i = 0; i < n; ++i)
            cout << a + i << ": "
                 << (float)p[i] / (float)(num_of_exp)
                 << endl;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Expected probability: 0.1
    uniform_int_distribution (0, 9)
    0: 0.0993
    1: 0.1007
    2: 0.0998
    3: 0.0958
    4: 0.1001
    5: 0.1049
    6: 0.0989
    7: 0.0963
    8: 0.1026
    9: 0.1016
    

    We could observe from the output that the probability of each number obtained from the random number is much closer to calculated probability.

  2. a(): Returns the lower parameter of range. This specifies the lower bound of the range of values potentially returned by its member operator().
  3. b(): Returns the higher parameter of range. This specifies the upper bound of the range of values potentially returned by its member operator().
  4. max(): This function return the possible smallest upper bound of output possible from the operator() function.
  5. min(): This function return the possible highest lower bound of output possible from the operator() function.
  6. reset(): This function resets the distribution such that subsequent distributions are not dependent on the previously generated numbers.

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_int_distribution function
#include <random>
  
using namespace std;
  
int main()
{
    int a = 10, b = 100;
  
    // Initializing of uniform_int_distribution class
    uniform_int_distribution<int> 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()
    distribution.reset();
    return 0;
}

chevron_right


Output:

Lower Bound 10
Upper Bound 100
Minimum possible output 10
Maximum possible output 100

Reference: https://en.cppreference.com/w/cpp/numeric/random/uniform_int_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 : AyushShukla8