Possible Duplicate:
Count Frequency in a Randomly Generated List of Numbers
I am currently working with arrays and random numbers. I have a created a form that will let me generate random numbers from 1 – 20 with a textbox1 to choose the quantity of numbers displayed. I am displaying the results inside a multiline textbox2. I have been able to calculate the sum and average of the set of numbers generated.
My second step:
Is there away to tally or mark the times a number is generated and display it in the multiline textbox(last picture)? Would I need to create an array and make it full of zero?
private void button1_Click(object sender, EventArgs e)
{
int n = Convert.ToInt32(textBox1.Text);
int[] y = new int[n];
double sum = 0;
for (int i = 0; i < n; i++)
{
int x = 1 + r.Next(20);
y[i] = x;
sum += x;
if (n < 101)
textBox2.AppendText(x + " ");
}
double avg = sum / n;
textBox2.AppendText(Environment.NewLine + sum + " " + avg + Environment.NewLine);
double vsum = 0;
for (int i = 0; i < n; i++)
vsum += (y[i] - avg) * (y[i] - avg);
}
Form.cs

Count the times an integer is randomly selected

Here’s the code that can do what you want:
Running this with a value of
25I got this result:Is that what you’re after?