C# 蒙特卡洛π Monte Carlo PI 随机数圆周率

链接: https://pan.baidu.com/s/1a0gKj0qZucgFlkNOUKuQog
提取码: 3qr7

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

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;

namespace 蒙特卡洛PI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int n1 = 0, n2 = 0,n4=0;
        private void button1_Click(object sender, EventArgs e)
        {

            Random r = new Random();
            Random s = new Random(10);
            
            for (int n = 1; n < int.Parse(textBox1.Text); n++)
            {
                
                double p = r.NextDouble();
                double q = s.NextDouble();
                double c;
                c = p * p + q * q;               
                if (c > 1)
                {
                    chart1.Series["正方形"].Points.AddXY(p, q);
                    n1 = n1 + 1;
                }
                else
                {
                    chart1.Series["圓"].Points.AddXY(p, q);
                    n2 = n2 + 1;
                }
            }
            double n3 = (double)4 * n2 / (n1 + n2);
            label2.Text = n3.ToString();

            n4 = n4 + int.Parse(textBox1.Text);
            label3.Text = n4.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            chart1.ChartAreas.Clear(); //图表区
            chart1.Titles.Clear(); //图表标题
            chart1.Series.Clear(); //图表序列
            chart1.Legends.Clear(); //图表图例
            ChartArea chartArea = new ChartArea();
            chart1.ChartAreas.Add(chartArea);
            chartArea.AxisX.MajorGrid.Enabled = false;
            chartArea.AxisY.MajorGrid.Enabled = false;
            chart1.Series.Add("正方形");
            chart1.Series.Add("圓");
            chart1.Series["正方形"].ChartType = SeriesChartType.FastPoint;
            chart1.Series["圓"].ChartType = SeriesChartType.FastPoint;
            chart1.Series["正方形"].Color = Color.Blue;
            chart1.Series["圓"].Color = Color.Red;
            Legend tu_li = new Legend();
            chart1.Legends.Add(tu_li);
            tu_li.Alignment = StringAlignment.Center;
            tu_li.Docking = Docking.Top;
            chart1.Series["正方形"].LegendText = "正方形";
            chart1.Series["圓"].LegendText = "圓";
        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值