Random number distribution C++11

Catalog

1. Random number

2, engine

2.1 type of engine

2.2 member functions of engine

2.3 test

3, distribution

3.1 types of distribution

3.2 member functions of distribution

Refer to the second edition of C + + standard library

1. Random number

1) Engine: the source of randomness, used to generate random numbers;

2) Distribution: random value, random range of random number;

3) Random number: use the engine to randomly select a value within the distribution range.

2, engine

2.1 type of engine

Foundation engine
linear_congruential_engine
mersenne_twister_engine
subtract_with_carry_engine

//Engine adapter
discard_block_engine
independent_bits_engine
shuffle_order_engine

//Engine adapter with parameters
minstd_rand
minstd_rand0
mt19937
mt19937_64
ranlux24_base
ranlux48_base
ranlux24
ranlux48
knuth_b

default_random_engine //The only engine with the same sequence generated on different platforms

2.2 member functions of engine

engine e; default construction
 engine e(s); build an engine in the state of S
 engine e(e2); replication construction
 e.seed(); set engine to initial state
 e.seed(s); set the engine to the state of S
 e(); returns a random value
 e.discard(n); advance N states
 e1 == e2; whether the states are equal
 E1! = E2; status is not equal
 OS < E; write the state of e to output stream os
 Is > > e; read a state from input stream is and put it into e

2.3 test

#include <iostream>
#include <random>
#include <algorithm>
#include <vector>
#include <sstream>

using namespace std;

int main()
{
	//Engine: the source of randomness.
	default_random_engine dre;

	//Integer. The resulting random number is distributed in [10,20]
	//Parameter 1 can be 0 by default, and the maximum value of parameter 2 is the maximum value of numeric_limits < type >:: max() type
	uniform_int_distribution<int> di(10, 20);

	for ( int i = 0; i < 20; ++i)
	{
		cout << di(dre) << ends;
	}
	cout << endl;

	//Floating point type. The values are distributed in the [10.0,20.0) range
	uniform_real_distribution<double> dr(10, 20);

	for ( int i = 0; i < 8; ++i)
	{
		cout << dr(dre) << ends;
	}
	cout << endl;

	//Test save stream status
	stringstream state;
	state << dre;
	for ( int i = 0; i < 3; ++i)
	{
		cout << dr(dre) << ends;
	}
	cout << endl;
	
	state >> dre;
	for (int i = 0; i < 3; ++i)
	{
		cout << dr(dre) << ends;
	}
	cout << endl;

	vector<int> myVec = { 1, 2, 3, 4, 5, 6, 7, 8 };

	//Shuffle the cards. Rearrange elements with default? Random? Engine
	shuffle(myVec.begin(), myVec.end(), dre);

	for ( auto x : myVec)
	{
		cout << x << ends;
	}
	cout << endl;

	//Engines created at the same time, randomly
	//shuffle(myVec.begin(), myVec.end(), default_random_engine());

	system("pause");
}
//16 13 20 19 14 17 10 16 15 14 12 13 13 18 10 15 12 13 12 17
//17.2584 19.8111 11.0986 17.9811 12.9703 10.0478 11.1246 16.3976
//18.7843 15.0366 17.9793
//18.7843 15.0366 17.9793
//7 5 1 8 4 2 3 6
//Please press any key to continue

3, distribution

3.1 types of distribution

uniform distribution     
             Uniform? Int? Distribution integer
             Uniform? Real? Distribution floating point

Bernoulli distribution    
              Bernoulli? Distribution Boolean
              Binary distribution integer
              Geometric distribution integer
              Negative binary distribution integer
 Poisson distribution
              Poisson distribution integer
              Exponential distribution floating point
              Gamma distribution floating point
              Weibull distribution floating point
              Extreme? Value? Distribution floating point
 Normal distribution
              Normal distribution floating point
              Lognormal? Distribution floating point
              Chi square distribution floating point
              Cauchy? Distribution floating point
              Fisher? Distribution floating point
              Student? Distribution floating point
 Sampling distribution
              Discrete? Distribution integer
              Pipewise & constant & distribution floating point
              Pipewise linear distribution floating point

3.2 member functions of distribution

Distr:: result'u type
 distr d; default construction
 distr d(args); with args as the parameter, create the distribution
 d(e); returns a random value. Values in d-region of random distribution of propulsion engine
 d.min(); returns the minimum value
 d.max(); returns the maximum value
 d1 == d2; judge whether the states are equal
 D1! = D2; judge whether the status is unequal
 OS < d; writes the state of d to the output stream
 Is > > d; read status from input stream to d
 distr::param_type; parameter type
 distr d(pt); create a distribution and parameterize it with param_type pt
 d.param(); returns the current parameterized type
 d(e,pt); returns the next value according to the engine and parameter type, and advances the engine
 d.param; returns the value of param

3.3 test

#include <iostream>
#include <random>
#include <algorithm>
#include <string>
 
using namespace std;

template <typename Distr, typename Eng>
void distr(Distr d, Eng e, const string & name)
{
	cout << name << ":" << endl;
	cout << "min() = " << d.min() << endl;
	cout << "max() = " << d.max() << endl;
	cout << "values : " << d(e) << ends
		<< d(e) << ends
		<< d(e) << ends
		<< d(e) << endl;
}

int main()
{
	knuth_b e;

	uniform_real_distribution<> ud(0, 10);
	distr(ud, e, "uniform_real_distribution");

	normal_distribution<> nd;
	distr(nd, e, "normal_distribution");

	exponential_distribution<> ed;
	distr(ed, e, "normal_distribution");

	gamma_distribution<> gd;
	distr(gd, e, "normal_distribution");

	system("pause");
}
//uniform_real_distribution:
//min() = 0
//max() = 10
//values : 8.30965 1.30427 9.47764 3.83416
//normal_distribution :
//min() = 4.94066e-324
//max() = 1.79769e+308
//values : 0.117963 - 0.131724 0.538967 - 0.140331
//normal_distribution :
//min() = 0
//max() = 1.79769e+308
//values : 1.77765 0.139753 2.95199 0.48356
//normal_distribution :
//min() = 4.94066e-324
//max() = 1.79769e+308
//values : 1.77765 0.139753 2.95199 0.48356
//Please press any key to continue

 

43 original articles published, praised 53, visited 6661
Private letter follow

Posted by ligraci on Mon, 24 Feb 2020 04:35:43 -0800