Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用随机字符串ASP.NET替换行_C#_Asp.net_Asp.net Mvc_Visual Studio 2010_Visual Studio - Fatal编程技术网

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();