Showing posts with label Random Numbers. Show all posts
Showing posts with label Random Numbers. Show all posts

Monday, May 19, 2025

uniform_int_distribution

Overview
This is a random number distribution class that generates sequences of pseudo-random numbers from pseudo-random number engines.

Details
Syntax
//IntType:A integer type.
template <class IntType = int> 
class uniform_int_distribution;

member types
Following properties are defined.
NameDefinition
uint_least32_t result_typeThe type of the numbers generated.
param_typeThe type returned by param method.

Constructor
NameDescription
  1. uniform_int_distribution
    (result_type a = 0,
    result_type b = numeric_limits<result_type>::max())
  2. uniform_int_distribution
    (const param_type& parm)
Constructs a uniform_int_distribution object, and initializes its internal state value:
  1. Constructs based on the lower bound a and upper bound b.
  2. Constructs based on the param input.
Example
    //1
    uniform_int_distribution<long> di(1,100);
    uniform_int_distribution<> di2(1,100);

    //2
    uniform_int_distribution<long> di3(di.param());
    uniform_int_distribution<> di4(di2.param());

Methods
NameDescription
result_type min()

Returns the lower bound  of the returned value. i.e., a

Example
  uniform_int_distribution<int> di(1,100);
  //prints 1
  cout << di.min() << endl;
 result_type max()
Returns the upper bound  of the returned value. i.e., b

Example
  uniform_int_distribution<int> di(1,100);
  //prints 100
  cout << di.max() << endl;
  1. param_type param()
  2. void param
    (const param_type& parm)
  1. Returns an object with the parameters currently associated with the distribution object.
  2. Associates the parameters in object parm to the distribution object.
Example
  uniform_int_distribution<int> di(1,100);

  //1
  auto p = di.param();

  //2
  uniform_int_distribution<int> di2;
  di2.param(p);
  1. result_type operator()
    (URNG& g)
  2. result_type operator()
    (URNG& g, const param_type& parm)
Generates random numbers that are distributed according to the associated probability function. The entropy is acquired by calling g.operator().
  1. Uses the associated parameter set
  2. Uses params. The associated parameter set is not modified.
Example
  default_random_engine g(20160921);

  //1
  uniform_int_distribution<int> di(1,100);
  //prints 79
  cout << di(g) << endl;

  //2
  uniform_int_distribution<int> di2;
  //prints 96
  cout << di2(g,di.param()) << endl;
void reset()
Resets the distribution, so that subsequent uses of the object do not depend on values already produced by it.
result_type  a()Returns lower bound of the distribution parameter.
Example
  uniform_int_distribution<int> di(1,100);
  //prints 1
  cout << di.a() << endl;
result_type b()Returns upper bound of the distribution parameter.
Example
  uniform_int_distribution<int> di(1,100);
  //prints 100
  cout << di.b() << endl;

External Methods
NameDescription
ostream& operator<<
(ostream& os, const  uniform_int_distribution& di )

Saves internal state of di to os.

Example
    uniform_int_distribution<int> di(1,100);
    stringstream ss;
    ss << di;
   //prints 1 100
    cout << di << endl;
istream& operator>>
(istream&  is, uniform_int_distribution& di )
Restores internal state of di from is. 

Example
    uniform_int_distribution<int> di(1,100);
    stringstream ss;
    ss << di;
   //prints 1 100
cout << di << endl;
   //prints 1 100
ss >> di; cout << di << endl;
bool operator==
(const uniform_int_distribution&  di,  const  uniform_int_distribution& di2)
Returns true if di and di2 are the same.
bool operator!=
(const  uniform_int_distribution& di, const  uniform_int_distribution& di2)
Returns true if di and di2 are not the same.

random number distributions

Overview
Multiple classes are provided to generate pseudo random numbers sequences. These classes use a pseudo random number engine to generate numbers and later distribute them into sequences using mathematical functions.

Details
A random number distribution is a function object that, when called with a random number generator
argument, produces a sequence of values of its result_type which can be any arithmetic type and Boolean.
IntType can be any of shortintlonglong longunsigned shortunsigned intunsigned long, or unsigned long long.
RealType can be any of floatdouble, or long double.

The distributions can be classified as below.
CategoryNameDefinition
Uniformuniform_int_distribution➹uniform_int_distribution<IntType>(IntType a, IntType b)
Example
/*prints

uniform_int_distribution:
0-1: *********
1-2: **********
2-3: *********
3-4: *********
4-5: **********
5-6: **********
6-7: *********
7-8: *********
8-9: **********
9-10: **********

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  uniform_int_distribution<int> distribution(0,9);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "uniform_int_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Uniformuniform_real_distribution➹uniform_real_distribution<RealType>(RealType a, RealType b)
Example
/*prints

uniform_real_distribution:
0-1: ****************************************************************************************************
1-2: 
2-3: 
3-4: 
4-5: 
5-6: 
6-7: 
7-8: 
8-9: 
9-10: 

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  uniform_real_distribution<double> distribution(0.0,1.0);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "uniform_real_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Bernoullibernoulli_distribution➹bernoulli_distribution<bool>(double p)
Example
/*prints

bernoulli_distribution:
0-1: **************************************************
1-2: *************************************************
2-3: 
3-4: 
4-5: 
5-6: 
6-7: 
7-8: 
8-9: 
9-10: 

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  bernoulli_distribution distribution(0.5);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "bernoulli_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Bernoullibinomial_distribution➹binomial_distribution<IntType>(IntType t, double p)
Example
/*prints

binomial_distribution:
0-1: 
1-2: *
2-3: ******
3-4: ***************
4-5: *************************
5-6: ************************
6-7: ****************
7-8: *******
8-9: *
9-10:

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  binomial_distribution<int> distribution(9,0.5);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "binomial_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Bernoulligeometric_distribution➹geometric_distribution<IntType>(double p)
Example
/*prints

geometric_distribution:
0-1: *****************************
1-2: ********************
2-3: ***************
3-4: **********
4-5: *******
5-6: ****
6-7: ***
7-8: **
8-9: *
9-10: *

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  geometric_distribution<int> distribution(0.3);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "geometric_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Bernoullinegative_binomial_distribution➹negative_binomial_distribution<IntType>(IntType k, double p)
Example
/*prints

negative_binomial_distribution:
0-1: ************
1-2: *******************
2-3: *****************
3-4: ****************
4-5: ***********
5-6: *******
6-7: *****
7-8: ***
8-9: **
9-10: *

*/    


  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  negative_binomial_distribution<int> distribution(3,0.5);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "negative_binomial_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Rate-basedpoisson_distribution➹poisson_distribution<IntType>(double mean)
Example
/*prints

poisson_distribution:
0-1: *
1-2: ******
2-3: **************
3-4: *******************
4-5: *******************
5-6: ****************
6-7: ***********
7-8: ******
8-9: ***
9-10: *

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  poisson_distribution<int> distribution(4.1);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "poisson_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Rate-basedexponential_distribution➹exponential_distribution<RealType>(RealType lambda)
Example
/*prints

exponential_distribution:
0-1: ************************************************************************************************
1-2: ***
2-3: 
3-4: 
4-5: 
5-6: 
6-7: 
7-8: 
8-9: 
9-10: 

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  exponential_distribution<double> distribution(3.5);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "exponential_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Rate-basedgamma_distribution➹gamma_distribution<RealType>(RealType alpha, RealType beta)
Example
/*prints

gamma_distribution:
0-1: *********
1-2: *****************
2-3: ******************
3-4: **************
4-5: ************
5-6: *********
6-7: *****
7-8: ****
8-9: ***
9-10: **

*/    
int main()
{
  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  gamma_distribution<double> distribution(2.0,2.0);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "gamma_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Rate-basedweibull_distribution➹weibull_distribution<RealType>(RealType a, RealType b)
Example
/*prints

weibull_distribution:
0-1: *****
1-2: ****************
2-3: ********************
3-4: ********************
4-5: ***************
5-6: *********
6-7: *****
7-8: **
8-9: *
9-10: 

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  weibull_distribution<double> distribution(2.0,4.0);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "weibull_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Rate-basedextreme_value_distribution➹extreme_value_distribution<RealType>(RealType a, RealType b)
Example
/*prints

extreme_value_distribution<double> distribution:
0-1: ********
1-2: *********
2-3: *********
3-4: ********
4-5: *******
5-6: *******
6-7: *****
7-8: *****
8-9: ****
9-10: ***

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
 extreme_value_distribution<double> distribution(2.0,4.0);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "extreme_value_distribution<double> distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Normalnormal_distribution➹normal_distribution<RealType>(RealType mean, RealType stddev)
Example
/*prints

normal_distribution<double> distribution):
0-1: *
1-2: ****
2-3: *********
3-4: ***************
4-5: ******************
5-6: *******************
6-7: ***************
7-8: ********
8-9: ****
9-10: * 

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  normal_distribution<double> distribution(5.0,2.0);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "normal_distribution<double> distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Normallognormal_distribution➹lognormal_distribution<RealType>(RealType m, RealType s)
Example
/*prints

lognormal_distribution<double> distribution(0.0,1.0):
0-1: **************************************************
1-2: **************************
2-3: **********
3-4: *****
4-5: **
5-6: *
6-7: 
7-8: 
8-9: 
9-10: 

*/    
int main()
{
  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  lognormal_distribution<double> distribution(0.0,1.0);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "lognormal_distribution<double> distribution(0.0,1.0):" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Normalchi_squared_distribution➹chi_squared_distribution<RealType>(RealType n)
Example
/*prints

chi_squared_distribution<double> distribution:
0-1: *******************
1-2: ***********************
2-3: ******************
3-4: ************
4-5: *********
5-6: *****
6-7: ***
7-8: **
8-9: *
9-10: *

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  chi_squared_distribution<double> distribution(3.0);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "chi_squared_distribution<double> distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Normalcauchy_distribution➹cauchy_distribution<RealType>(RealType a, RealType b)
Example
/*prints

cauchy_distribution<double> distribution:
0-1: *
1-2: **
2-3: ****
3-4: **********
4-5: ************************
5-6: *************************
6-7: *********
7-8: ****
8-9: **
9-10: *

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  cauchy_distribution<double> distribution(5.0,1.0);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "cauchy_distribution<double> distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Normalfisher_f_distribution➹fisher_f_distribution<RealType>(RealType m, RealType n)
Example
/*prints

fisher_f_distribution:
0-1: *************************************************
1-2: ****************
2-3: ********
3-4: *****
4-5: ***
5-6: **
6-7: *
7-8: *
8-9: *
9-10: *

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  fisher_f_distribution<double> distribution(2.0,2.0);

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "fisher_f_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Normalstudent_t_distribution➹student_t_distribution<RealType>(RealType n)
Example
/*prints

student_t_distribution:
-3.0..-2.5: 
-2.5..-2.0: **
-2.0..-1.5: ****
-1.5..-1.0: ********
-1.0..-0.5: **************
-0.5.. 0.0: ******************
 0.0.. 0.5: *******************
 0.5.. 1.0: **************
 1.0.. 1.5: ********
 1.5.. 2.0: ****
 2.0.. 2.5: **
 2.5.. 3.0: *

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  // intervals definitions:
  const int nintervals=12;
  const double first=-3.0;
  const double span=0.5;

  default_random_engine generator;
  student_t_distribution<double> distribution(10.0);

  int p[nintervals]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=first)&&(number<first+nintervals*span))
      ++p[int((number-first)/span)];
  }

  cout << "student_t_distribution:" << endl;
  cout << fixed; cout.precision(1);

  for (int i=0; i<nintervals; ++i) 
  {
    cout.width(4); cout << (first+i*span) << "..";
    cout.width(4); cout << (first+(i+1)*span) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Piecewisediscrete_distribution➹discrete_distribution<IntType>(size_t nw, double xmin, double xmax, UnaryOperation fw);
Example
/*prints

discrete_distribution:
0-1: ************
1-2: *************
2-3: *****
3-4: ******
4-5: ************
5-6: ************
6-7: ******
7-8: ******
8-9: ************
9-10: ************
9-10: ************ 

*/    
  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  discrete_distribution<int> distribution {2,2,1,1,2,2,1,1,2,2};

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "discrete_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Piecewisepiecewise_constant_distribution➹piecewise_constant_distribution<RealType>(InputIt first_i, InputIt last_i, InputIt2 first_w)
Example
/*prints

piecewise_constant_distribution:
0-1: ************
1-2: *************
2-3: *****
3-4: ******
4-5: ************
5-6: ************
6-7: ******
7-8: ******
8-9: ************
9-10: ************ 

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  array<double,6> intervals {0.0, 2.0, 4.0, 6.0, 8.0, 10.0};
  array<double,5> weights {2.0, 1.0, 2.0, 1.0, 2.0};
  piecewise_constant_distribution<double>
    distribution (intervals.begin(),intervals.end(),weights.begin());

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "piecewise_constant_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }
Piecewisepiecewise_linear_distribution➹piecewise_linear_distribution<RealType>(InputIt first_i, InputIt last_i, InputIt2 first_w)
Example
/*prints

piecewise_linear_distribution:
0-1: *******************
1-2: **************
2-3: *********
3-4: *****
4-5: *
5-6: *****
6-7: **********
7-8: **************
8-9: *******************
9-10: 

*/    

  const int nrolls=10000;  // number of experiments
  const int nstars=100;    // maximum number of stars to distribute

  default_random_engine generator;
  array<double,3> intervals {0.0, 4.5, 9.0};
  array<double,3> weights {10.0, 0.0, 10.0};
  piecewise_linear_distribution<double>
    distribution (intervals.begin(),intervals.end(),weights.begin());

  int p[10]={};

  for (int i=0; i<nrolls; ++i) 
  {
    double number = distribution(generator);
    if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
  }

  cout << "piecewise_linear_distribution:" << endl;

  for (int i=0; i<10; ++i) 
  {
    cout << i << "-" << (i+1) << ": ";
    cout << string(p[i]*nstars/nrolls,'*') << endl;
  }

interface
All engines distributions provide same interface except for parameters. Constructor might differ for based on parameters. The following describes the interface for uniform_int_distribution➹.