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 writeRandom Random=new Random()的可能重复的指导原则编辑您的问题
在函数GetWord
之外,然后您将获得所需的输出。
static void Man(string[] args)
{
Console.WriteLine(RandomString(100));
}