#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;
}