C#生成符合正态分布的随机数并绘制图形

1.效果图

 

 

 

 

 

 

 

 

 

2.源码比较简单直接贴了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Threading;
namespace TEST
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataColumn dctime = new DataColumn("尺寸", Type.GetType("System.String"));
            DataColumn dcCity = new DataColumn("个数", Type.GetType("System.String"));
            ReadDBInforAuto.Columns.Add(dctime);
            ReadDBInforAuto.Columns.Add(dcCity);
            ThreadStart ts = new ThreadStart(RunMain);
            Thread td = new Thread(ts);
            td.Start();
        }
        Dictionary<double, int> dicNum = new Dictionary<double, int>();
        List<double> ListNum = new List<double>();
        void RunMain()
        {
            while (true)
            {
                double data =Math.Round( NormalDistribution()[0],1);
                if (ListNum.Contains(data))
                {
                    dicNum[data] += 1;
                }
                else
                {
                    ListNum.Add(data);
                    ListNum.Sort();
                    dicNum.Add(data,1);
                }
                DrawLine();
                Thread.Sleep(50);
            }
        }
        public static double[] NormalDistribution()
        {

            Random rand = new Random();
            double[] y;
            double u1, u2, v1=0, v2=0, s = 0, z1=0, z2=0;
            while (s > 1 || s == 0)
            {
                u1 = rand.NextDouble();
                u2 = rand.NextDouble();
                v1 = 2 * u1 - 1;
                v2 = 2 * u2 - 1;
                s = v1 * v1 + v2 * v2;
            }
            z1 = Math.Sqrt(-2 * Math.Log(s) / s) * v1;
            z2 = Math.Sqrt(-2 * Math.Log(s) / s) * v2;
            y = new double[] { z1, z2 };
            return y; //返回两个服从正态分布N(0,1)的随机数z0 和 z1
        }
        DataTable ReadDBInforAuto = new DataTable();
        void DrawLine()
        {
            BeginInvoke(new Action(() =>
            {
                DataTable tableInfo = new DataTable();
                DataColumn dctime = new DataColumn("尺寸", Type.GetType("System.String"));
                DataColumn dcOK = new DataColumn("个数", Type.GetType("System.String"));
                tableInfo.Columns.Add(dctime);
                tableInfo.Columns.Add(dcOK);
                foreach (double ke in ListNum) 
                {
                    DataRow dr1 = tableInfo.NewRow();
                    dr1["尺寸"] = ke;
                    dr1["个数"] = dicNum[ke];
                    tableInfo.Rows.Add(dr1);
                }
                chart1.DataSource = tableInfo;
                chart1.ChartAreas[0].AxisX.Title = "尺寸";
                chart1.ChartAreas[0].AxisY.Title = "个数";
                //chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0%";
                // Set series members names for the X and Y values
                chart1.Series["个数"].XValueMember = "尺寸";
                chart1.Series["个数"].YValueMembers = "个数";
                // Data bind to the selected data source
                chart1.DataBind();
                // Set series chart type
                chart1.Series["个数"].ChartType = SeriesChartType.StackedColumn;

                chart1.Series["个数"].IsValueShownAsLabel = false;

                // Enable X axis margin
                chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true;
                chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
                // Enable 3D, and show data point marker lines
                //chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
                chart1.Series["个数"]["ShowMarkerLines"] = "True";
                chart1.Series["个数"].ToolTip = "个数:#VAL\r尺寸:#AXISLABEL";
            }));
        }
    }
}

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值