c# - c#中的随机名称生成器

标签 c# random names

我有一个女性和男性名字列表,然后是数组中的姓氏列表。

我试图做的是使用随机生成器在这些数组中获取这些名称,并根据我的调用输出随机的名字和姓氏。

完成后,我打算在其他类中引用该方法,而不必每次都写出来。

这是我目前的代码:

  private void RandName()
  {
        string[] maleNames = new string[1000] { "aaron", "abdul", "abe", "abel", "abraham", "adam", "adan", "adolfo", "adolph", "adrian"};
        string[] femaleNames = new string[1000] { "abby", "abigail", "adele", "adrian"};
        string[] lastNames = new string[1000] { "abbott", "acosta", "adams", "adkins", "aguilar"};

        Random rand = new Random(DateTime.Now.Second);
        if (rand.Next(1, 2) == 1)
        {
            FirstName = maleNames[rand.Next(0, maleNames.Length - 1)];
        }
        else
        {
            FirstName = femaleNames[rand.Next(0, femaleNames.Length - 1)];
        }

  }

我的问题是:如何使用我创建的名称数组创建随机名称生成器?

最佳答案

    public static string GenerateName(int len)
    { 
        Random r = new Random();
        string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "l", "n", "p", "q", "r", "s", "sh", "zh", "t", "v", "w", "x" };
        string[] vowels = { "a", "e", "i", "o", "u", "ae", "y" };
        string Name = "";
        Name += consonants[r.Next(consonants.Length)].ToUpper();
        Name += vowels[r.Next(vowels.Length)];
        int b = 2; //b tells how many times a new letter has been added. It's 2 right now because the first two letters are already in the name.
        while (b < len)
        {
            Name += consonants[r.Next(consonants.Length)];
            b++;
            Name += vowels[r.Next(vowels.Length)];
            b++;
        }

        return Name;


     }

关于c# - c#中的随机名称生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14687658/

相关文章:

c# - 匹配以随机内容开头的可选模式

c# - 如何获取属性的支持字段(AssociatedPropertyOrEvent 为空)?

python - Python 中 numpy.random 和 random.random 的区别

c++ - 快速安全地确定范围内的随机数

r - 在 R 中的行名称中使用向量的值?

c# - 使用 char** 参数的 PInvoke 中出现问题,字符串数组不起作用

c# - Alexa TopSites - 连续签名失败 - C# 实现

javascript - 基于 JavaScript 文件上的 random() 执行不同的函数

java - 盲目获取Java类名?

python - 赋值 : Prompt user to input their Birthmonth then input the day they were born. 打印月份名称,后跟日期