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.