0

I'm trying to generate random numbers depending on the user Input. So if the user inputs 10 then I want to generate 10 random numbers.I'm getting the user input from a textbox.

   private void GenerateRandomNumber()
            {

                int count = 1;

                //Int32.TryParse(GeneratetextBox.Text, out UserNumber);
                UserNumber = Convert.ToInt32(GeneratetextBox.Text);

                ///creates random object to produce random values 
                Random randomNumbers = new Random();

                while (count == UserNumber)
                {
                    //UserNumber = randomNumbers.Next(100);

                    listBox.Items.Add(randomNumbers.Next(100));

                    count = count + 1;
                }

                //for ( count = UserNumber; count == UserNumber; count++)
                //{
                //    UserNumber = randomNumbers.Next(100);

                //    listBox.Items.Add(randomNumbers.Next(100));

                //    count = count + 1;
                //}
    }
6
  • 2
    ` while (count == UserNumber)` when is this true? Commented Sep 17, 2016 at 8:41
  • 1
    OK, what's your question?
    – Phil K
    Commented Sep 17, 2016 at 8:42
  • I'm trying to generate random numbers according to the user input. So if the user inputs 30 then i should generate 30 random numbers.
    – Becca
    Commented Sep 17, 2016 at 8:46
  • Read this: ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Sep 17, 2016 at 8:50
  • From your comment to Tiomon's post it's not clear if you are trying to get 30 random numbers or should they be smaller then 30?
    – Igor
    Commented Sep 17, 2016 at 9:03

2 Answers 2

2

Try this to genearate random numbers:

        private void GenerateRandomNumber()
        {                
            int userNumber= Convert.ToInt32(GeneratetextBox.Text);

            Random randomNumbers = new Random();

             // loop until i is not les than userNumber

            for (int i = 0; i < userNumber; i++){  
                  // generate random number and add it to the list.
                  int randNumber= randomNumbers.Next(100);
                  listBox.Items.Add(randNumber);
            }
          }

          private void button1_Click(object sender, EventArgs e)
          {
             // clear listbox before generating new numbers
             listBox.Items.Clear();
             GenerateRandomNumber();
           }

If your are triggering this method with a button don't forget to clear your listbox before generating new randnubers.

2
  • I get the randoms numbers but they are either more than the user input.
    – Becca
    Commented Sep 17, 2016 at 8:54
  • @Becca just made a edit, don't forget to clear your listbox otherwise it will add the random numbers to the listbox and will have more values than the user wanted.
    – Timon Post
    Commented Sep 17, 2016 at 9:11
1

Your while loops stops when count is different from UserNumber, so since you initialize count = 1 you need to have UserNumber = 1 if you want the code in the while to be executed.

So, you need to fix the while condition:

private void GenerateRandomNumber() {

            int count = 1;

            //Int32.TryParse(GeneratetextBox.Text, out UserNumber);
            UserNumber = Convert.ToInt32(GeneratetextBox.Text);

            ///creates random object to produce random values 
            Random randomNumbers = new Random();

            while (count <= UserNumber)
            {
                //UserNumber = randomNumbers.Next(100);

                listBox.Items.Add(randomNumbers.Next(100));

                count = count + 1;
            }

            //for ( count = UserNumber; count == UserNumber; count++)
            //{
            //    UserNumber = randomNumbers.Next(100);

            //    listBox.Items.Add(randomNumbers.Next(100));

            //    count = count + 1;
            //}
}

That way the code inside the while will stop being executed when UserNumber is greater than count, which is what you are looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.