文章目录

  • 一、前言
  • 二、正态分布
  • 1、代码
  • 2、结果
  • 三、均匀分布
  • 1、代码
  • 2、结果
  • 四、梯形分布
  • 1、简介
  • 2、代码
  • 3、结果
  • 五、其他


一、前言

在实验研究和工作中,处理数据是一个不可或缺的步骤。然而,在数据处理软件的开发过程中,经常需要特定的数据,来验证我们的程序。因此,如何创建模拟数据变得极为重要。
在生成模拟数据中,最常见的就是生成一系列特定分布类型的数据,譬如正态分布等。为此,本文将介绍正态分布、均匀分布、梯形分布三种分布类型的随机数据的生成程序。

二、正态分布

1、代码

在程序中,开放了数据生成个数、正态分布的数据参数,用户根据文本提示,可在控制台手动输入。
程序如下:

#include <iostream>
    #include <cmath>
    #include <ctime>
    #include <cstdlib>
    #include <random>
    #include <stdio.h>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <map>
    
    using namespace std;

    int main()
    {
        //正态分布
        int n;
        double mean ;                                         // 均值
        double sigma ;                                        // 标准差
        cout << "正态分布:" << endl;
        cout << "生成数据个数:" << endl;
        cin >> n;
        cout <<"输入均值:" << endl;
        cin >> mean;
        cout << "输入标准差:" << endl;
        cin >> sigma;
        default_random_engine generator(time(NULL));
        normal_distribution<double> distribution(mean, sigma);

        //打开文件,生成并写入
        ofstream ofs;
        ofs.open("resultzhengtai.txt", ios::out);
        for (int i = 0; i < n; i++) 
        {
            double number = distribution(generator);
            if (number >= 0 && number <= 100)
            { 
                ofs << fixed << setprecision(1) << number;
                ofs << "\n";
            }   
        }
        ofs.close();
    
        return 0;   
    }

2、结果

  • 运行及输入:运行程序,根据文本提示依次输入“生成数据个数”、“均值”、“标准差”
    输入
  • 生成文件:在VS当前解决方案的路径下,生成了记录着生成的数据的文本文件。

此时,用excel打开上述文本文件,并将数据用直方图显示,可见确实为正态分布,对应参数也与输入一致。



java 正态分布曲线 如何编正态分布数据_java 正态分布曲线


数据直方图


三、均匀分布

1、代码

同上,开放了数据生成个数、均匀分布的数据参数。
程序如下:

#include <iostream>
    #include <cmath>
    #include <ctime>
    #include <cstdlib>
    #include <random>
    #include <stdio.h>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <map>
    
    using namespace std;

    int main()
    {
        //均匀分布
        int m;
        int uplimit;                                               // 区间上限
        int downlimit;                                             // 区间下限
        double num[3000];
        cout << "均匀-生成个数" << endl;
        cin >> m;
        cout << "输入区间下限" << endl;
        cin >> downlimit;
        cout << "输入区间上限" << endl;
        cin >> uplimit;
    
        srand(time(0));                                     // 用当前时间作为随机种子
        for (int i = 0; i < m; i++) 
        {
             num[i] = (rand() % (uplimit*10- downlimit*10) + downlimit*10)/float(10); 
             cout << num[i] << endl;
        }
        ofs.open("resultjunyun.txt", ios::out);
        for (int i = 0; i < m; i++)
        {
            ofs << fixed << setprecision(3) << num[i];
            ofs << "\n";
        }
    
        return 0;   
    }

2、结果

  • 运行及输入:运行程序,根据文本提示依次输入“生成数据个数”、“区间下限”、“区间上限”
    输入
  • 生成文件:在VS当前解决方案的路径下,生成了记录着生成的数据的文本文件。

此时,用excel打开上述文本文件,并将数据用直方图显示,可见确实为均匀分布,对应参数也与输入一致。



java 正态分布曲线 如何编正态分布数据_c++_02


数据直方图


四、梯形分布

1、简介

梯形分布较为特殊,其定义为:测量值的出现机会在中间各处一样,在两边直线下降,在边缘为零则称其服从梯形分布。其概率密度函数为:
java 正态分布曲线 如何编正态分布数据_#include_03



java 正态分布曲线 如何编正态分布数据_数据_04


梯形分布


若测量值服从梯形分布, 则其期望java 正态分布曲线 如何编正态分布数据_java 正态分布曲线_05标准差java 正态分布曲线 如何编正态分布数据_java 正态分布曲线_06

2、代码

同上,开放了数据生成个数、梯形分布的数据参数。
程序如下:

#include <iostream>
    #include <cmath>
    #include <ctime>
    #include <cstdlib>
    #include <random>
    #include <stdio.h>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <map>
    
    using namespace std;
    
    //定义梯形分布的概率密度函数
    double trapezoidal_pdf(double x, double a, double b, double c, double d)
    {
        if (x < a || x > d)
        {
            return 0.0;
        }
        else if (x >= a && x <= b)
        {
            return (x - a) / (b - a);
        }
        else if (x >= b && x <= c)
        {
            return 1.0;
        }
        else
        {
            return (d - x) / (d - c);
        }
    }
    
    int main()
    {
        //梯形分布
        srand(time(NULL));
        int N;
        double a  ;
        double b  ;
        double c  ;
        double d  ;
    
        cout << "梯形-生成个数" << endl;
        cin >> N;
        cout << "输入总区间下限" << endl;
        cin >> a;
        cout << "输入总区间上限" << endl;
        cin >> d;
        cout << "输入均匀区间下限" << endl;
        cin >> b;
        cout << "输入均匀区间上限" << endl;
        cin >> c;
    
        double num2[3000];
        for (int i = 0; i < N; i++)
        {
            double x = rand() / (double)RAND_MAX * (d - a) + a;
            double p = trapezoidal_pdf(x, a, b, c, d);
            double y = rand() / (double)RAND_MAX;
            if (y <= p)
            {
                cout << x << endl;
                num2[i] = x;
            }
            else
            {
                i--;
            }
        }
        ofstream ofs;
        ofs.open("resulttixing.txt", ios::out);
        for (int i = 0; i < N; i++)
            {
                ofs << fixed << setprecision(3) << num2[i];
                ofs << "\n";
            }

         return 0;
    }

3、结果

  • 运行及输入:运行程序,根据文本提示依次输入“生成数据个数”、“总区间下限”、“总区间上限”、“均匀区间下限”、“均匀区间上限”
    输入
  • 生成文件:在VS当前解决方案的路径下,生成了记录着生成的数据的文本文件。

此时,用excel打开上述文本文件,并将数据用直方图显示,可见确实为梯形分布,对应参数也与输入一致。



java 正态分布曲线 如何编正态分布数据_c++_07


数据直方图


五、其他

今天需要用到PS,本人电脑没有安装,偶然想到猿如意工具栏有,试用了一下,不用下载巨大的安装包,果真方便。