VOOZH about

URL: https://www.geeksforgeeks.org/dsa/stduniform_int_distribution-class-in-c/

⇱ std::uniform_int_distribution class in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

std::uniform_int_distribution class in C++

Last Updated : 11 Jul, 2025

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: 

 

👁 Image
👁 Image


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 probability.
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: 
     

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. 
 

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


Example:
 


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.html.html
 

Comment