随机数分布C++11

目录

1、随机数

2、引擎

2.1 引擎的种类

2.2 引擎的成员函数

2.3 测试

3、分布

3.1 分布的种类

3.2 分布的成员函数


资料参考《C++标准库》第二版

1、随机数

1)引擎:随机性的源头,用来产生随机数;

2)分布:随机值,随机数的随机范围;

3)随机数:利用引擎在分布范围内随机选一个值。

2、引擎

2.1 引擎的种类

基础引擎
linear_congruential_engine
mersenne_twister_engine
subtract_with_carry_engine

引擎适配器
discard_block_engine
independent_bits_engine
shuffle_order_engine

带参数的引擎适配器
minstd_rand
minstd_rand0
mt19937
mt19937_64
ranlux24_base
ranlux48_base
ranlux24
ranlux48
knuth_b

default_random_engine //唯一不同平台上生成的数列相同的引擎

2.2 引擎的成员函数

engine e;             默认构造
engine e(s);          以s的状态建立一个引擎
engine e(e2);         复制构造
e.seed();             将引擎设置为初始状态
e.seed(s);            将引擎设置为s的状态
e();                  返回一个随机值
e.discard(n);         前进n个状态
e1 == e2;             状态是否相等
e1 != e2;             状态是否不相等
os << e;              将e的状态写入output stream os
is >> e;              从input stream is 中读取一个状态放入e

2.3 测试

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

using namespace std;

int main()
{
	//引擎:随机性的源头。
	default_random_engine dre;

	//整数。产生的随机数分布在[10,20]
	//参数1可以默认为0,参数2最大值为 numeric_limits<type>::max()类型的最大值
	uniform_int_distribution<int> di(10, 20);

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

	//浮点型。数值分布在[10.0,20.0)区间
	uniform_real_distribution<double> dr(10, 20);

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

	//测试保存流状态
	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 };

	//洗牌。利用default_random_engine重新排列元素
	shuffle(myVec.begin(), myVec.end(), dre);

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

	//同一时间创建的引擎,随机方式是相同的
	//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
//请按任意键继续. . .

3、分布

3.1 分布的种类

均匀分布     
             uniform_int_distribution        整型
             uniform_real_distribution       浮点型

伯努利分布    
              bernoulli_distribution         布尔
              binomial_distribution          整型
              geometric_distribution         整型
              negative_binomial_distribution 整型
泊松分布
              poisson_distribution           整型
              exponential_distribution       浮点型
              gamma_distribution             浮点型
              weibull_distribution           浮点型
              extreme_value_distribution     浮点型
正态分布
              normal_distribution            浮点型
              lognormal_distribution         浮点型
              chi_squared_distribution       浮点型
              cauchy_distribution            浮点型
              fisher_f_distribution          浮点型
              student_t_distribution         浮点型
抽样分布
              discrete_distribution          整型
              piecewise_constant_distribution浮点型
              piecewise_linear_distribution  浮点型

3.2 分布的成员函数

distr::result_type;         类型
distr d;                    默认构造
distr d(args);              以args为参数,创建分布
d(e);                       返回随机值。推进引擎随机分布d区域中的值
d.min();                    返回最小值
d.max();                    返回最大值
d1 == d2;                   判断 状态是否相等
d1 != d2;                   判断,状态是否不相等
os << d;                    将d的状态写入输出流
is >> d;                    从输入流中读取状态给d
distr::param_type;          参数类型
distr d(pt);                创建一个分布,以param_type pt完成参数化
d.param();                  返回当前参数化类型
d(e,pt);                    根据 引擎 和 参数类型,返回下一个值,并推进引擎
d.param;                    返回参数param的值

3.3 测试

#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
//请按任意键继续. . .

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章