0

I'm needing to create random numbers based on the number of random numbers needed by a user.

The user can then save those random numbers to a file when the save to file button is clicked.

After that, the program allows the user to open the same file, but this time, the file opened needs to display how many random numbers within a specific range were created. The ranges are: (0-19, 20-39, 40-59, 60-79, and 80-99).

So far I'm able to create one random number and list the same number over the number of times entered by the user, but unfortunately:

  • The random numbers created need to all be random through the number of iterations created by the user.

  • I don't know how to display the numbers within their respective range.

  • I can't display the numbers properly.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
        }
    
        private void RandNumbtextBox_TextChanged(object sender, EventArgs e)
        {
    
    
        }
    
        private void saveFileButton_Click(object sender, EventArgs e)
        {
            double randomNumber = double.Parse(RandNumbtextBox.Text);
            Random r = new Random();//random number object
            int random = r.Next(1, 10);// random number 1
            if (saveFileDialog.ShowDialog()== DialogResult.OK)
            {
                StreamWriter outputFile;
                outputFile = File.CreateText(saveFileDialog.FileName + ".txt");
                for(int i = 0; i < randomNumber; i++)
                {
                    outputFile.WriteLine(random.ToString());
                }
    
                outputFile.Close();
    
            }
            else
            {
                MessageBox.Show("op cancelled");
            }
    
    
        }
    
        private void openFileButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK) 
            {
                StreamReader inputFile;
                inputFile = File.OpenText(openFileDialog.FileName);
                int sum = 0;
                while (!inputFile.EndOfStream)
                {
                    sum += int.Parse(inputFile.ReadLine());
                }
                inputFile.Close();
                MessageBox.Show(sum.ToString());
            }            
            else
            {
                    MessageBox.Show("op cancelled");
            }
        }
    }
    

    Thanks to John I was able to figure out the first issue of the question. Generating the random number issue and write them to file. I'm still unable to: *Display the numbers within their respective range.

  • I can't display the numbers properly.

1 Answers1

1

OP, you are assigning the random number once:

int random = r.Next(1, 10);// random number 1

And then writing it out randomNumber times:

for(int i = 0; i < randomNumber; i++)
{
    outputFile.WriteLine(random.ToString());
}

I think your code needs to read:

for(int i = 0; i < randomNumber; i++)
{
    outputFile.WriteLine(r.Next(1, 10).ToString());
}

As for counting numbers in ranges: A simple approach might be to create "count" variables for each range, and implement branching logic to increment the correct one depending on the number you're looking at. A more dynamic approach may involve a dictionary, or an array of classes which contain the range and a count value.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • omg!!!!! Thank you so much!!! I couldn't figure it out. Any ideas on the second part of my issue. The opening the file into specific ranges – Christian Hernandez Dec 11 '18 at 01:13
  • You can get the line count at the same time as you're reading the file, then use that to determine the range... – Rufus L Dec 11 '18 at 01:15
  • `string range; if (lineCount < 20) range = "0-19"; else if (lineCount < 40) range = "20-39"; else if (lineCount < 60) range = "40-59"; else range = "80-99";` – Rufus L Dec 11 '18 at 01:18