当前位置: 代码迷 >> 综合 >> C# 蒙特卡洛π Monte Carlo PI 随机数圆周率
  详细解决方案

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

热度:90   发布时间:2023-10-13 12:36:10.0

载请注明出处,联系我: t39q@163.com
本人热衷于数据库技术及算法的研究,志同道合之士, 欢迎探讨

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

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

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

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){
    }}
}