Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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# 随机单词选择器选择相同字母的序列?_C#_Random_Console Application_Generator - Fatal编程技术网

C# 随机单词选择器选择相同字母的序列?

C# 随机单词选择器选择相同字母的序列?,c#,random,console-application,generator,C#,Random,Console Application,Generator,我正在做一个简单的刽子手游戏,在控制台上运行。我创建了一个方法,从列表中选择随机单词,稍后作为答案实现。我创建此方法就是为了这样做: public static string GetWord() { Random random = new Random(); string[] words = new string[5]{"a", "b", "c", "d", "e"}; return words[random.Next(5)]; } 我通过使用for循环将该方法循环10

我正在做一个简单的刽子手游戏,在控制台上运行。我创建了一个方法,从列表中选择随机单词,稍后作为答案实现。我创建此方法就是为了这样做:

public static string GetWord()
{
    Random random = new Random();
    string[] words = new string[5]{"a", "b", "c", "d", "e"};
    return words[random.Next(5)];
}
我通过使用for循环将该方法循环100次来测试该方法:

static void Man(string[] args)
{
    for(int i = 0; i <101; i++)
    {
        Console.WriteLine(GetWord());
    }
}
静态void Man(字符串[]args)
{
对于(int i=0;i请尝试以下方法:

private static readonly Random Random = new Random();

public static string RandomString(int length)
{
    const string chars = "abcde";
    return new string(Enumerable.Repeat(chars, length).Select(s => s[Random.Next(s.Length)]).ToArray());
}


欢迎来到StackOverflow。您的问题不太符合StackOverflow期望的标准。您当前的问题可能不会被接受。我强烈建议您按照try to write
Random Random=new Random()的可能重复的指导原则编辑您的问题
在函数
GetWord
之外,然后您将获得所需的输出。
static void Man(string[] args)
{
   Console.WriteLine(RandomString(100));
}