正態分佈發生器和方差產生模擬器

#include <boost/random.hpp>
#include <time.h>
#include <iostream>
#include <sstream>
#include <fstream>

using namespace boost;
using namespace std;

// 通過隨機數發生器產生樣本模擬差生方差
void manualSigma()
{
	boost::mt19937 engine(time(0));

	for (;;)
	{
		int sampleNums = 0;

		double mean = 0.f;		// 期望
		double leftV = 0;
		double rightV = 0;

		std::cout << "輸入樣本數:" ;
		std::cin >> sampleNums;
		std::cout << std::endl;

		std::cout << "輸入期  望:" ;
		std::cin >> mean;
		std::cout << std::endl;

		std::cout << "輸入左  值:" ;
		std::cin >> leftV;
		std::cout << std::endl;

		std::cout << "輸入右  值:" ;
		std::cin >> rightV;
		std::cout << std::endl;


		double sigma = 0.f;	// 方差
		uniform_real<> dist(leftV, rightV);

		int largeThanMeanNum = 0;
		int equalThanMeanNum = 0;
		int smallThanMeanNum = 0;
		double maxValue = mean;
		double minValue = mean;

		int eNums = sampleNums;
		double sum = 0;
		while (eNums > 0)
		{
			double v = dist(engine);

			sum += sqrt(abs(v - mean));

			if (v > mean)
				++largeThanMeanNum;
			else if (v < mean)
				++smallThanMeanNum;
			else
				++equalThanMeanNum;

			if (v > maxValue)
				maxValue = v;
			if (v < minValue)
				minValue = v;

			--eNums;
		}

		sigma = sum / sampleNums;

		std::cout << "方      差      爲:" << sigma << std::endl;
		std::cout << "大於期望的值的總數:" << largeThanMeanNum << std::endl;
		std::cout << "等於期望的值的總數:" << equalThanMeanNum << std::endl;
		std::cout << "小於期望的值的總數:" << smallThanMeanNum << std::endl;
		std::cout << "樣本中所得最大的值:" << maxValue << std::endl;
		std::cout << "樣本中所得最小的值:" << minValue << std::endl;

		std::cout  << std::endl;
		std::cout  << std::endl;
		std::cout  << std::endl;
	}
}

// 正態分佈發生器
void generateND(void)
{
	int sampleNums = 0;
	double mean = 0.f;		// 期望
	double sigma = 0.f;		// 方差

	std::cout << "輸入樣本數:" ;
	std::cin >> sampleNums;
	std::cout << std::endl;

	std::cout << "輸入期  望:" ;
	std::cin >> mean;
	std::cout << std::endl;

	std::cout << "輸入方  差:" ;
	std::cin >> sigma;
	std::cout << std::endl;
	
	mt19937 rng(time(0));
	normal_distribution<double> * nd2 = new normal_distribution<double>(mean, sigma);

	typedef boost::variate_generator<boost::mt19937&, boost::normal_distribution<double> > GenType;
	GenType gen(rng, *nd2); 

	std::stringstream ss;
	int largeThanMeanNum = 0;
	int equalThanMeanNum = 0;
	int smallThanMeanNum = 0;
	double maxValue = mean;
	double minValue = mean;
	
	for (int i = 0; i < sampleNums; ++i)
	{
		//cout << gen() << endl;
		double v = gen();
		ss << v << "  ";

		if (v > mean)
			++largeThanMeanNum;
		else if (v < mean)
			++smallThanMeanNum;
		else
			++equalThanMeanNum;

		if (v > maxValue)
			maxValue = v;
		if (v < minValue)
			minValue = v;
	}

	ss << '\n';
	ss << "樣 本 總 數 爲:" << sampleNums << '\n';
	ss << "樣本中最大值爲:" << maxValue<< '\n';
	ss << "樣本中最小值爲:" << minValue<< '\n';
	ss << "大於期望的值的總數:" << largeThanMeanNum << '\n';
	ss << "等於期望的值的總數:" << equalThanMeanNum << '\n';
	ss << "小於期望的值的總數:" << smallThanMeanNum << '\n';

	std::fstream file;
	file.open("ND.txt", ios_base::binary|ios_base::in | ios_base::out|ios::trunc );

	file.write(ss.str().c_str(), ss.str().size());

	file.close();


	std::cout << "所有樣本已經保存在文件‘ND.txt’內!" << std::endl;

	delete nd2;
}


int main(int argc, char ** argv)
{
	int way = 0;
	std::cout << "請輸入數字:1)方差模擬產生器;    2)正態分佈樣本發生器" << std::endl;
	std::cin >> way;

	if (way == 1)
	{
		manualSigma();
	}
	else if (way == 2)
	{
		generateND();
	}

	return 0;
}

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