C# 用随机字符串ASP.NET替换行
我想用随机字符串替换文本文件中的多行。这是我的密码C# 用随机字符串ASP.NET替换行,c#,asp.net,asp.net-mvc,visual-studio-2010,visual-studio,C#,Asp.net,Asp.net Mvc,Visual Studio 2010,Visual Studio,我想用随机字符串替换文本文件中的多行。这是我的密码 public static string Random(int ran) { string _allowedChars = "abcdefghijkmnopqrstuvwxyz0123456789"; Random randNum = new Random(); char[] chars = new char[ran]; int allowedCharCount = _allowe
public static string Random(int ran)
{
string _allowedChars = "abcdefghijkmnopqrstuvwxyz0123456789";
Random randNum = new Random();
char[] chars = new char[ran];
int allowedCharCount = _allowedChars.Length;
for (int i = 0; i < ran; i++)
{
chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
}
return new string(chars);
}
protected void Button1_Click(object sender, EventArgs e)
{
string filePath = "try2.txt";
string[] lines = File.ReadAllLines(filePath);
for (int i = 0; i < lines.Length; i += 2)
{
lines[i] = lines[i].Replace("1", Random(int.Parse("5")));
}
File.WriteAllLines(filePath, lines);
}
公共静态字符串随机(int-ran)
{
字符串_allowedChars=“abcdefghijkmnopqrstuvwxyz012456789”;
Random randNum=新的Random();
char[]chars=新字符[ran];
int allowedCharCount=\u allowedChars.Length;
for(int i=0;i
但它只生成1个随机字符串,并每2行添加一个
我想每两行生成不同的随机字符串
我想不出来。尝试创建一次
Random randNum
,而不是每次都重新创建它
例如
如果两者之间有时差
Random randNum = new Random();
如果种子太小,则可能使用了相同的种子。请稍帮帮忙:(
Random randNum = new Random();