Open In App

random header in C++ | Set 3 (Distributions)

Improve
Improve
Like Article
Like
Save
Share
Report
We have discussed generators in Set 1. We have also discussed three distributions in Set 2. In this post, more distributions are discussed. IV.Rate-based Distributions:
poisson_distribution Poisson Distribution
exponential_distribution Exponential Distribution
gamma_distribution Gamma Distribution
weibull_distribution Weibull Distribution
extreme_value_distribution Extreme Value Distribution
1. poisson_distribution: It is a distribution that produces integers according to a Poisson distribution, which is given by the following probability mass function:  P\left ( i|\mu  \right ) = \frac{\mu^{i}}{i!}e ^{-\mu},i\geq 0
  • operator():It returns a new random number that follows the distribution’s parameters.
  • min:It returns the greatest lower bound of the range of values given by operator(), which in this case is always zero.
  • max: It returns the least upper bound of the range of values given by operator().
  • reset: It resets the distribution, so that subsequent uses of the object do not depend on values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of operator() in 
    // poisson_distribution 
    #include <iostream>
    #include <chrono>
    #include <random>
    using namespace std;
      
    // Driver Program
    int main()
    {
      // construct a trivial random generator 
      // engine from a time-based seed:
      unsigned seed = 
         chrono::system_clock::now().time_since_epoch().count();
      default_random_engine generator (seed);
      
      poisson_distribution<int> distribution (7.1);
      
      cout << "Poisson-distribution(mean=5.0): ";
      for (int i=0; i<10; ++i)
        
        // use of operator()
        cout << distribution(generator) << " ";
      
      cout << endl;
      
      return 0;
    }
    
                        
    Output:
    Poisson-distribution(mean=5.0): 11 5 5 9 10 6 15 3 6 5 
    
    2. exponential_distribution: It is a random number distribution that produces floating-point values according to an exponential distribution, given by:  p\left ( x|\lambda   \right ) = \lambda e^{- \lambda x}, x\g>0
  • operator():It generates the random number that are distributed according to the probability function.
  • max: It returns the least upper bound of the range of given by operator().
  • max:It returns the greatest lower bound of the range given by operator(), which is always zero for exponential_distribution.
  • reset:It resets the distribution, so that on the subsequent uses the result does not depends on the values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of  operator() in 
    // exponential_distribution 
    #include <iostream>
    #include <chrono>
    #include <thread>
    #include <random>
    using namespace std;
      
    // Driver program
    int main()
    {
      // It constructs a trivial random 
      // generator engine from a time-based seed
      int seed = 
        chrono::system_clock::now().time_since_epoch().count();
      default_random_engine generator (seed);
      
      exponential_distribution<double> distribution (1.0);
        
      cout << "Hi's separated by 2 seconds, on average: \n";
      for (int i=0; i<5; ++i) 
      {      
        // use of operator()
        double number = distribution(generator);
        chrono::duration<double> period (number);
          
        // It makes the thread sleep 
        // for the time period(i.e. number)
        this_thread::sleep_for( period );
        cout << "Hi,Geeks!!" << endl;
      }
      
      return 0;
    }
    
                        
    Output:
    Hi's separated by 2 seconds, on average: 
    Hi,Geeks!!
    Hi,Geeks!!
    
    3. gamma_distribution: It is a random number distribution that produces floating-point values according to a gamma distribution, given by:  p \left ( x | \alpha, \beta \right ) = \frac{1}{\Gamma ( \alpha ). \beta ^{ \alpha }} . x^{ \alpha -1} . e^\frac{-x}{ \beta } , x \geq 0
  • operator():It returns a new random number that follows the distribution’s parameters.
  • min:It returns the greatest lower bound of the range given by member operator(), which is always zero for gamma_distribution.
  • max:It returns the least upper bound of the range given by operator().
  • reset:It resets the distribution, so that on the subsequent uses the result does not depend on values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of reset in gamma_distribution
    #include <iostream>
    #include <random>
    using namespace std;
       
    // Driver program
    int main()
    {     
      // Random number generator
      default_random_engine generator;
      gamma_distribution<double> distribution(1.0,2.0);
       
      // prints first random number
      cout << distribution(generator) << endl;
         
      // use of reset 
      distribution.reset();
         
      // prints the second random number 
      // independent of first
      cout << distribution(generator) << endl;
       
      return 0;
    }
    
                        
    Output:
    1.14392
    2.23359
    
    4. weibull_distribution:It is a random number distribution that produces floating-point values according to a 2-parameter Weibull distribution,given by:  p \left ( x | a, b \right ) = \frac{a}{b} . \left ( \frac{x}{b} \right )^ {a-1} . e^{-\left ( \frac{x}{b} \right )^a} , x \geq 0
  • operator():It returns a new random number that follows the distribution’s parameters.
  • min:It returns the greatest lower bound of the range given by operator(), which is always zero for weibull_distribution.
  • max:It returns the least upper bound of the range given by operator().
  • reset:It resets the distribution, so that on the subsequent uses the result does not depends on the values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of min and max
    // in weibull_distribution
    #include <iostream>
    #include <chrono>
    #include <random>
    using namespace std;
      
    // Driver program
    int main ()
    {   
      // It constructs a trivial random 
      // generator engine from a time-based seed
      unsigned seed = 
        chrono::system_clock::now().time_since_epoch().count();
      default_random_engine generator (seed);
      
      weibull_distribution<double> distribution (2.0,1.0);
      
      cout << distribution(generator) 
           << " is a random number between ";
        
      // use of min and max
      cout << generator.min() << " and " << generator.max();
        
      return 0;
    }
    
                        
    Output:
    1.54229 is a random number between 1 and 2147483646
    
    5. extreme_value_distribution:It is a random number distribution that produces floating-point values according to a Type I extreme value distribution,given by:  p \left ( x | a, b \right ) = \frac{1}{b} . z(x). e^{-z(x)} \ where \ z(x) = e^{ \frac{a-x}{b}}
  • operator():It generates the random number that are distributed according to the probability function.
  • min:It returns the greatest lower bound of the range given by member operator().
  • max:It returns the least upper bound of the range given by member operator().
  • reset:It resets the distribution, so that on the subsequent uses the result does not depends on the values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of param in 
    // extreme_value_distribution
    #include <iostream>
    #include <random>
    using namespace std;
      
    // Driver program
    int main()
    {
      default_random_engine generator;
      extreme_value_distribution<double> d1(2.0,4.0);
      extreme_value_distribution<double> d2(d1.param());
      
      // prints the first value
      cout << d1(generator) << endl;
        
      // prints the second independent value 
      cout << d2(generator) << endl;
      
      return 0;
    }
    
                        
    Output:
    9.8351
    3.95306
    
    V. Related to Normal distribution
    normal_distribution Normal Distribution
    lognormal_distribution Lognormal Distribution
    chi_squared_distribution Chi-squared Distribution
    cauchy_distribution Cauchy Distribution
    fisher_f_distribution Fisher F-Distribution
    student_t_distribution Student T-Distribution
    1. normal_distribution: It is a random number distribution that produces floating-point values according to a normal distribution, given by:  p \left ( x | \mu, \sigma \right ) = \frac{1}{\sigma\sqrt{2\pi}} . e^{-\frac{(x-\mu)^{2}}{2 \sigma^{2}}}
    where:
     (µ) :mean 
     sigma :standard deviation
    
  • operator():It generates the random number that are distributed according to the probability function.
  • min:It returns the greatest lower bound of the range given by operator().
  • max:It returns the least upper bound of the range given by operator().
  • reset:It resets the distribution, so that on the subsequent uses the result does not depends on the values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of operator()
    // in normal_distribution 
    #include <iostream>
    #include <chrono>
    #include <random>
    using namespace std;
      
    // Driver program
    int main()
    {
      // It constructs a trivial random 
      // generator engine from a time-based seed
      unsigned seed = 
       chrono::system_clock::now().time_since_epoch().count();
      default_random_engine generator (seed);
      
      // Initializes the normal distribution
      normal_distribution<double> distribution (1.0,2.0);
      
      cout << "Normal-distribution(1.0,2.0):" << endl;
      for (int i=0; i<5; i++)  
        // Use of  operator()
        cout << distribution(generator) << endl;
      
      return 0;
    }
    
                        
    Output:
    Normal-distribution(1.0,2.0):
    1.59499
    -0.458303
    1.34411
    0.138838
    3.03433
    
    2. lognormal_distribution:It is a random number distribution that produces floating-point values according to a lognormal distribution, given by:  p \left ( x | m, s \right ) = \frac{1}{sx\sqrt{2\pi}} . e^{-\frac{(lnx-m)^{2}}{2 s^{2}}}, x \geq 0
  • operator():It generates a random number which follows this distribution.
  • min:It returns the greatest lower bound of the range given by operator(), which is always zero for lognormal_distribution.
  • max:It returns the least upper bound of the range given by operator().
  • reset:It resets the distribution, so that on the subsequent uses the result does not depends on the values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of reset in 
    // lognormal_distribution
    #include <iostream>
    #include <random>
    using namespace std;
      
    // Driver program
    int main()
    {
      // the random number generator
      default_random_engine generator;
      lognormal_distribution<double> distribution(1.0,2.0);
      
      // prints first value:
      cout << distribution(generator) << endl;
        
      // Use of reset
      distribution.reset();
        
      // prints second value independent of first
      cout << distribution(generator) << endl;
      
      return 0;
    }
    
                        
    Output:
    2.12989
    10.6822
    
    3. chi_squared_distribution:It is a random number distribution that produces floating-point values according to a chi-squared distribution,given by:  p \left ( x | n \right ) = \frac{1}{\Gamma \left ( \frac{n}{2} \right ).2^{\frac{n}{2}}}.x^{\frac{n}{2}-1}. e^{-\frac{x}{2}}, x \geq 0
    where,
    n : degrees of freedom and n>0,
    n/2 : shape parameter
    
  • operator():It generates the random number that are distributed according to the probability function.
  • min:It returns the greatest lower bound of the range given by operator(), which is always zero for the chi_squared_distribution.
  • max:It returns the least upper bound of the range given by operator().
  • reset:It resets the distribution, so that on the subsequent uses the result does not depends on the values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of operator() in 
    // chi_squared_distribution 
    #include <iostream>
    #include <chrono>
    #include <random>
    using namespace std;
      
    // Driver program
    int main()
    {
      // It constructs a trivial random 
      // generator engine from a time-based seed
      unsigned seed = 
       chrono::system_clock::now().time_since_epoch().count();
      default_random_engine generator (seed);
      
      chi_squared_distribution<double> distribution (4.0);
      
      cout << "chi-squared-distribution(4.0):" << endl;
      for (int i=0; i<5; i++)
          
        // use of operator()
        cout << distribution(generator) << endl;
      
      return 0;
    }
    
                        
    Output:
    chi-squared-distribution(4.0):
    2.18701
    6.86953
    1.77983
    9.79626
    5.04758
    
    4. cauchy_distribution:It is a random number distribution that produces floating-point values according to a Cauchy distribution, given by:  p \left ( x | a,b \right ) = \frac{1}{\pi b . \left [ 1 +  \left ( \frac{x-a}{b} \right )^{2} \right ] }
    where,
    a and b are distribution parameters
    
  • operator():It generates the random number that are distributed according to the probability function.
  • min:It returns the greatest lower bound of the range given by operator().
  • max:It returns the least upper bound of the range given by operator().
  • reset: It resets the distribution, so that on the subsequent uses the result does not depends on the values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of param
    // in cauchy_distribution
    #include <iostream>
    #include <random>
    using namespace std;
      
    // Driver program
    int main()
    {
      default_random_engine generator;
      cauchy_distribution<double> d1(0.0,1.0);
      cauchy_distribution<double> d2(d1.param());
      
      // prints the first value
      cout << d1(generator) << endl;
        
      // prints the second value
      cout << d2(generator) << endl;
      
      return 0;
    }
    
                        
    Output:
    0.438486
    7.65462
    
    5. fisher_f_distribution:It is a random number distribution that produces floating-point values according to a Fisher F-distribution, given by:  p \left ( x | m,n \right ) = \frac{\Gamma \left ( \frac{m+n}{2} \right )}{\Gamma\left ( \frac{m}{2} \right ). \Gamma \left ( \frac{n}{2} \right )}. \frac{\left ( \frac{mx}{n} \right )^{\frac{m}{2}}}{x.\left ( 1 + \frac{mx}{n} \right )^{\frac{m+n}{2}}},\ x \geq 0  It produces random numbers by dividing two independent Chi-squared distributions of m and n degrees of freedom.
  • operator():It generates the random number that are distributed according to the probability function.
  • min:It returns the greatest lower bound of the range given by operator(), which is always zero for the fisher_f_distribution.
  • max:It returns the least upper bound of the range given by operator().
  • reset: It resets the distribution, so that on the subsequent uses the result does not depends on the values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of 
    // operator() in fisher_f_distribution 
    #include <iostream>
    #include <chrono>
    #include <random>
    using namespace std;
      
    // Driver program
    int main()
    {
      // It constructs a trivial random generator engine
      // from a time-based seed
      unsigned seed = 
        chrono::system_clock::now().time_since_epoch().count();
      default_random_engine generator (seed);
      
      fisher_f_distribution<double> distribution (1.0,2.0);
      
      cout << "fisher-f-distribution(1.0,2.0):" << endl;
      for (int i=0; i<5; i++)
          
        // use of operator()
        cout << distribution(generator) << endl;
      
      return 0;
    }
    
                        
    Output:
    fisher-f-distribution(1.0,2.0):
    0.208066
    2.76882
    0.0305701
    0.96243
    0.444256
    
    6. student_t_distribution:It is a random number distribution that produces floating-point values according to a Student T-distribution, given by:  p \left ( x | n \right ) = \frac{1}{\sqrt{n \pi}}.\frac{\Gamma\left (\frac{n+1}{2} \right )}{\Gamma \left ( \frac{n}{2}\right )}.\left (1\ +\ \frac{x^{2}}{n} \right )^{-\frac{n+1}{2}}
    where,
    n is the distribution parameter
    
  • operator():It generates the random number that are distributed according to the probability function.
  • min:It returns the greatest lower bound of the range given by operator().
  • max:It returns the least upper bound of the range given by operator().
  • reset: It resets the distribution, so that on the subsequent uses the result does not depends on the values already produced by it.
  • param: It gets or sets the distribution parameter object .
  • // Illustrating the use of min and max
    // in student_t_distribution
    #include <iostream>
    #include <chrono>
    #include <random>
    using namespace std;
       
    // Driver program
    int main ()
    {     
      // It constructs a trivial random 
      // generator engine from a time-based seed
      unsigned seed = 
       chrono::system_clock::now().time_since_epoch().count();
      default_random_engine generator (seed);
       
      student_t_distribution<double> distribution (8.0);
       
      cout << distribution(generator) 
           << " is a random number between ";
         
      // use of min and max
      cout << generator.min() << " and " << generator.max();
         
      return 0;
    }
    
                        
    Output:
    0.00906058 is a random number between 1 and 2147483646


    Last Updated : 12 Feb, 2018
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
    Similar Reads