Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8548891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:36:10+00:00 2026-06-11T13:36:10+00:00

Possible Duplicate: Count Frequency in a Randomly Generated List of Numbers I am currently

  • 0

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

enter image description here

Count the times an integer is randomly selected

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:36:11+00:00Added an answer on June 11, 2026 at 1:36 pm

    Here’s the code that can do what you want:

            var r = new Random();
            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[]
            {
                "Results",
                String.Format("{0} {1}", sum, avg),
            }.Concat(Enumerable
                .Range(1, 20)
                .Select(x => String.Format("{0} ({1}x)", x, frequency[x].Count()))));
    

    Running this with a value of 25 I got this result:

    Results
    278 11.12
    1 (0x)
    2 (2x)
    3 (1x)
    4 (1x)
    5 (1x)
    6 (2x)
    7 (0x)
    8 (1x)
    9 (2x)
    10 (4x)
    11 (1x)
    12 (0x)
    13 (0x)
    14 (0x)
    15 (2x)
    16 (1x)
    17 (2x)
    18 (2x)
    19 (3x)
    20 (0x)
    

    Is that what you’re after?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to count the frequency of the elements in a list? I
Possible Duplicate: Count(*) vs Count(1) If I have a table, 'id' is primary key,
Possible Duplicate: Count specific character occurances in string I have a delimeter in string
Possible Duplicate: How to count duplicates in Ruby Arrays Ruby: Compare 2 arrays for
Possible Duplicate: NSNumber retain count issue Hello, I have the following code: NSNumber *number
Possible Duplicate: SQL Count records within a month using a unix timestamp I have
Possible Duplicate: How to Count Duplicates in List with LINQ Any idea how do
Possible Duplicate: How to get line count cheaply in Python? Good day. i have
Possible Duplicate: Hibernate HQL: Get count of results without actually returning them I have
Possible Duplicate: How can I count the numbers of rows that a mysql query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.