将一系列随机数写入 C# 中的文件不起作用。正在打印相同的数字。我该如何解决这个问题?
Writing a series of random numbers to a file in C# not working. Same number being printed. How can I resolve the issue?
我正在尝试将一系列数字(取决于用户指定的内容)写入文件,但我的代码的结果是每次循环执行时打印的相同数字。这是我的代码:
private void saveButton_Click(object sender, EventArgs e)
{
// Get number for amount of random numbers to write
int number = int.Parse(numberTextBox.Text);
// Create a new saveFileDialog objects
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "Choose where to save the file";
saveFileDialog.Filter = "Text File | *.txt";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
StreamWriter writer = new StreamWriter(saveFileDialog.OpenFile());
int randNum;
Random random;
// Create a for loop to write to file
for (int i = 0; i < number; i++)
{
random = new Random();
randNum = random.Next(1, 101);
writer.WriteLine(randNum);
}
writer.Dispose();
}
}
``
这个代码块对我有用:
Do edit this according to your need. If you need futher elaboration.
please refer @Mike Perrenoud's elaboration here.
Random random = new Random();
Console.WriteLine("Please enter the name of the numbers file");
string fileLotto = Console.ReadLine();
//creating the lotto file
FileStream fs = new FileStream("../../" + fileLotto + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
BufferedStream bs = new BufferedStream(fs);
Console.WriteLine("File created");
fs.Close();
StreamWriter sw = new StreamWriter("../.." + fileLotto + ".txt");
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 7; j++)
{
//Console.Write(random.Next(1, 49));
sw.Write(random.Next(1, 49) + " " );
}
sw.WriteLine();
}
sw.Close();
我正在尝试将一系列数字(取决于用户指定的内容)写入文件,但我的代码的结果是每次循环执行时打印的相同数字。这是我的代码:
private void saveButton_Click(object sender, EventArgs e)
{
// Get number for amount of random numbers to write
int number = int.Parse(numberTextBox.Text);
// Create a new saveFileDialog objects
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "Choose where to save the file";
saveFileDialog.Filter = "Text File | *.txt";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
StreamWriter writer = new StreamWriter(saveFileDialog.OpenFile());
int randNum;
Random random;
// Create a for loop to write to file
for (int i = 0; i < number; i++)
{
random = new Random();
randNum = random.Next(1, 101);
writer.WriteLine(randNum);
}
writer.Dispose();
}
}
``
这个代码块对我有用:
Do edit this according to your need. If you need futher elaboration. please refer @Mike Perrenoud's elaboration here.
Random random = new Random();
Console.WriteLine("Please enter the name of the numbers file");
string fileLotto = Console.ReadLine();
//creating the lotto file
FileStream fs = new FileStream("../../" + fileLotto + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
BufferedStream bs = new BufferedStream(fs);
Console.WriteLine("File created");
fs.Close();
StreamWriter sw = new StreamWriter("../.." + fileLotto + ".txt");
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 7; j++)
{
//Console.Write(random.Next(1, 49));
sw.Write(random.Next(1, 49) + " " );
}
sw.WriteLine();
}
sw.Close();