Generating Random numbers: Counting the times x number has been randomly generated
<p>I am currently working with generating a random number(1-20) and counting the number of times each number has been randomly generated. In <code>textBox1</code> I choose the amount of numbers I want to generate. I display the final results in a multiline <code>textBox2</code>. The problem I am experiecing is that every time I click the button again it resets the count of times a number has been randomly generated. </p>

<p>Is there away i can click the button an x amount of times and count the times a number has been randomly generated without resetting the count? I am  trying to this specifically with the help of an array. </p>

<p><strong>Code</strong></p>

<pre><code> 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 =&gt; r.Next(20) + 1)
                    .ToArray();

            var sum = y.Sum();
            var avg = (double)sum / (double)n;
            var frequency = y.ToLookup(x =&gt; 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 =&gt; String.Format("{0} ({1})", x, frequency[x].Count()))));

        }

    }
</code></pre>
