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 8527759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:38:46+00:00 2026-06-11T08:38:46+00:00

I am currently working with generating a random number(1-20) and counting the number of

  • 0

I am currently working with generating a random number(1-20) and counting the number of times each number has been randomly generated. In textBox1 I choose the amount of numbers I want to generate. I display the final results in a multiline textBox2. 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.

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.

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 => 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 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-11T08:38:48+00:00Added an answer on June 11, 2026 at 8:38 am

    The scope of your variable lives only inside the button1_Click method. You need to move it out as a private class variable in order to persist it across clicks. It’ll require some minor refactoring of your code in order to get there.

    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;
        }
    }
    

    Note that the upper bound of the rand.next(min,max) method is exclusive, so that means in order to generate numbers from 1 to 20, you need to pass it 1 to 21. Not sure why the inconsistency there–it’s a little confusing.

    Output for n = 10

    Generated sequence: {12, 5, 12, 15, 8, 20, 6, 5, 16, 6}
    Number 1 generated 0 times.
    Number 2 generated 0 times.
    Number 3 generated 0 times.
    Number 4 generated 0 times.
    Number 5 generated 2 times.
    Number 6 generated 2 times.
    Number 7 generated 0 times.
    Number 8 generated 1 times.    
    Number 9 generated 0 times.
    Number 10 generated 0 times.
    Number 11 generated 0 times.
    Number 12 generated 2 times.
    Number 13 generated 0 times.
    Number 14 generated 0 times.
    Number 15 generated 1 times.
    Number 16 generated 1 times.
    Number 17 generated 0 times.
    Number 18 generated 0 times.
    Number 19 generated 0 times.
    Number 20 generated 1 times.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem has been KILLING me. I've been working on this app for 8
I'm currently generating a large number (100s) of SSIS packages from C# that are
I'm currently working on solving a bug with a site I've been working on
I'm working with generating .pdf's from PHP using this library: http://www.fpdf.org/ I am currently
I'm currently working on generating a PDF from a simple html page that contains
I am currently working on generating liquibase changelogs from an existing table in mySql.
I'm currently working on someone else's database where the primary keys are generated via
I use the following code for generating the random number from 0 to 15.
I am currently working with generating dynamic types using Reflection.Emit. I have the majority
I am currently working on a project that involves generating sequences using stored procedures

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.