简体   繁体   中英

Random Number Generator using C#

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM