0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Random_Number_Generator_In_Range
{

    public partial class Form1 : Form
    {
        int number;
        void random()
        {
            for (int count = 0; count < 20; count++)
            {
                Random rnd = new Random();
                number = rnd.Next(49) + 1;
                MessageBox.Show(number.ToString());
                if (number == 0 || number <= 24)
                {

                }
                else
                {

                }
            }
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            random();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}

Hello Everyone

This is my first ever post on Stack Overflow so if there are any issues with my question, please let me know so it can help me going forward.

So I have done a little bit of C++ programming, however, now I'm working through some tutorial questions within C# (Windows Application Form) but my knowledge is very limited.

The tutorial questions asks:

  1. Write a program that will generate 20 random integers in the range 0-49. The program should count and report how many were in the range 0-24 and how many were in the range 25-49. The Form should have one button labelled “Run Simulation” and two labels used to display the results of the simulation. The button and labels should be suitably labelled.

First and foremost, I'm not even sure if I'm on the right track or not, but I am not sure how to print the numbers 0-24 in label 1 and numbers 25-49 into label 2. I have created an if/else statement as I'm sure the instruction I need to enter should go into them but I can't for the life of me think what it would be.

Hopefully this is clear and any assistance would be appreciated.

4
  • 2
    The labels have a text property that you would use to add text to the labels. Label1.Text = "Some text"
    – Sorceri
    May 4, 2016 at 21:15
  • 1
    Not immediately related to your question, but your code will fail: Random number generator only generating one random number
    – Steve
    May 4, 2016 at 21:16
  • 1
    if (number == 0 || number <= 24) is identical to if (number <= 24). If that was a typo and you had wanted to write if (number >= 0 ...) instead, then the logical OR operator (||) is wrong as well. That should be a logical AND (&&). May 4, 2016 at 21:17
  • Doing Random rnd = new Random(); inside a loop is almost always wrong. See the question "Random number generator only generating one random number" if you got rid of the MessageBox.Show( you would have the same problem too. May 4, 2016 at 21:49

1 Answer 1

0

Welcome to the stack.

You could set two counter variables to hold the current count of numbers in 0-24 and 25-49 and then edit your if statement to do something like this..

if (number <= 24)
{
    countLowNumbers++;
}
else
{
    countHighNumbers++;
}

Then update both labels:

Label1.Text = countLowNumbers.ToString();
Label2.Text = countHighNumbers.ToString();

That should hopefully get you going.

7
  • Hello again. I inputted the 2 counter variables and then updated both labels, however, nothing what is supposed to happen, does. When I run the programme, and press button 1 on the Windows Application Form, it gives me 1 random integer at a time until it reaches the target of 20. When I click on the labels, it gives me only 1 integer and within the 25-49 field, I'm getting like 12 or 8 for example. May 4, 2016 at 22:34
  • The numbers in the labels should just be a count of how many of the random numbers fell into that range. You won't need the label click events, just update the label text contents at the end of your loop. The requirements above are a little muddled. Do you want to print the numbers generated or just a count of each number that fall into the ranges? May 4, 2016 at 22:49
  • I apologise I'm being an absolute idiot. I though the labels would list the list of integers, not how many fell into the specified ranges. I've just re-checked it and it is doing what it's supposed to (It's been a long day). One last question. One of the comments states that I should not put my random number generator inside of the for loop, however, when I place it anywhere other than inside the loop, it does not generate a random number. Can you explain why this is the case and why a random generator should not be inside a loop? May 4, 2016 at 23:00
  • I know the feeling!! You should move the declaration of Random (Random rnd = new Random()) outside the loop while leaving the .Next() line inside it. The stack overflow post quoted by Scott Chamberlain above explains it in some detail but in essence leaving your code as is can lead to the random generator giving you similar numbers each time May 4, 2016 at 23:19
  • Ahh, perfect. I will ammend the code tomorrow, bed time now. Thanks again. May 4, 2016 at 23:25

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.