C# 生成随机数:计算随机生成x数的次数

C# 生成随机数:计算随机生成x数的次数,c#,C#,我目前正在生成一个随机数(1-20),并计算每个数字随机生成的次数。在textBox1中,我选择要生成的数字量。我在多行textBox2中显示最终结果。我遇到的问题是,每次我再次单击按钮时,它都会重置随机生成数字的次数 我是否可以在不重置计数的情况下,单击按钮x次,然后计算随机生成的数字的次数?我正试图在数组的帮助下实现这一点 代码 public partial class Form1 : Form { public Form1() {

我目前正在生成一个随机数(1-20),并计算每个数字随机生成的次数。在
textBox1
中,我选择要生成的数字量。我在多行
textBox2
中显示最终结果。我遇到的问题是,每次我再次单击按钮时,它都会重置随机生成数字的次数

我是否可以在不重置计数的情况下,单击按钮x次,然后计算随机生成的数字的次数?我正试图在数组的帮助下实现这一点

代码

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Random r = new Random();

        private void button1_Click(object sender, EventArgs e)
        {


            var n = int.Parse(this.textBox1.Text);



            var y =
                Enumerable
                    .Range(0, n)
                    .Select(x => r.Next(20) + 1)
                    .ToArray();

            var sum = y.Sum();
            var avg = (double)sum / (double)n;
            var frequency = y.ToLookup(x => x);

            textBox2.Text = String.Join(Environment.NewLine, new[]
        {
            "Number of times an integer was randomly generated",
            String.Format("{0} {1}", sum, avg),
        }.Concat(Enumerable
                .Range(1, 20)
                .Select(x => String.Format("{0} ({1})", x, frequency[x].Count()))));

        }

    }

您需要记住实例字段中的一些数据。或者是以前在列表中生成的所有数字(这将引入很少的代码更改),或者是直方图本身作为
字典


实例字段在对事件处理程序的调用中保留,而局部变量丢失。

您需要记住实例字段中的一些数据。或者是以前在列表中生成的所有数字(这将引入很少的代码更改),或者是直方图本身作为
字典


实例字段在对事件处理程序的调用中被保留,而局部变量丢失。

变量的作用域仅存在于
按钮1\u Click
方法中。您需要将其作为私有类变量移出,以便在单击过程中保持它。为了达到这个目的,需要对代码进行一些小的重构

public partial class Form1 : Form
{
    private Random r = new Random();

    private int[] counts = new int[20];

    private static string newLine = Environment.NewLine;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (int.TryParse(this.textBox1.Text, out n))
        {
            // Clear the box
            this.textBox2.Text = string.Empty;

            var generatedList = new int[n];
            for (int i = 0; i < n; i++)
            {
                // Upper bound is EXCLUSIVE
                var gen = r.Next(1, 21);
                counts[gen - 1]++;
                generatedList[i] = gen;
            }

            this.textBox2.Text += PrintNumbers(generatedList);
            this.textBox2.Text += PrintCounts(this.counts);
        }
        else
        {
            this.textBox2.Text = "Invalid input! Cannot generate numbers.";
        }
    }

    private static string PrintNumbers(int[] numbers)
    {
        if (numbers == null)
        {
            return "No numbers generated" + newLine;
        }

        string result = "Generated sequence: {";
        for (int i = 0; i < numbers.Length; i++)
        {
            result += numbers[i];

            if (i < numbers.Length - 1)
            {
                result += ", ";
            }
        }

        return result + "}" + newLine;
    }

    private static string PrintCounts(int[] counts)
    {
        if (counts == null)
        {
            return string.Empty;
        }

        string result = string.Empty;
        for (int i = 0; i < counts.Length; i++)
        {
            result += "Number " + (i + 1) + " generated " + counts[i] + " times." + newLine;
        }

        return result;
    }
}

变量的范围仅存在于
按钮1\u Click
方法中。您需要将其作为私有类变量移出,以便在单击过程中保持它。为了达到这个目的,需要对代码进行一些小的重构

public partial class Form1 : Form
{
    private Random r = new Random();

    private int[] counts = new int[20];

    private static string newLine = Environment.NewLine;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (int.TryParse(this.textBox1.Text, out n))
        {
            // Clear the box
            this.textBox2.Text = string.Empty;

            var generatedList = new int[n];
            for (int i = 0; i < n; i++)
            {
                // Upper bound is EXCLUSIVE
                var gen = r.Next(1, 21);
                counts[gen - 1]++;
                generatedList[i] = gen;
            }

            this.textBox2.Text += PrintNumbers(generatedList);
            this.textBox2.Text += PrintCounts(this.counts);
        }
        else
        {
            this.textBox2.Text = "Invalid input! Cannot generate numbers.";
        }
    }

    private static string PrintNumbers(int[] numbers)
    {
        if (numbers == null)
        {
            return "No numbers generated" + newLine;
        }

        string result = "Generated sequence: {";
        for (int i = 0; i < numbers.Length; i++)
        {
            result += numbers[i];

            if (i < numbers.Length - 1)
            {
                result += ", ";
            }
        }

        return result + "}" + newLine;
    }

    private static string PrintCounts(int[] counts)
    {
        if (counts == null)
        {
            return string.Empty;
        }

        string result = string.Empty;
        for (int i = 0; i < counts.Length; i++)
        {
            result += "Number " + (i + 1) + " generated " + counts[i] + " times." + newLine;
        }

        return result;
    }
}

+1您能告诉我如何在阵列中使用此功能吗?根据您的评论。请稍候,完成文本框打印部分:)这太完美了!。是否每次单击后都要将其保存在一个显示结果中?更改
this.textBox2.Text+=打印结果(this.counts)
to
this.textBox2.Text=打印结果(this.counts)效果很好!谢谢,最后一件事:是否可以在
textBox2
中显示随机生成的数字行?+1您能告诉我如何在数组中显示该行吗?根据您的评论。请稍候,完成文本框打印部分:)这太完美了!。是否每次单击后都要将其保存在一个显示结果中?更改
this.textBox2.Text+=打印结果(this.counts)
to
this.textBox2.Text=打印结果(this.counts)效果很好!谢谢,最后一件事:是否可以在
textBox2
中显示随机生成的数字行?