随机生成人物名称

现在玩家越来越懒了。。名字输入是个很繁琐的事情,所以,我们需要提供一个按钮,玩家点击这个按钮,就会提供随机的名字,不满意继续点。。。

代码很简单,姓一个数组, 名一个数组,随机取就可以了

测试中发现Random如果不指定种子的话,使用默认很容易随机的结果都是一样的。所以取个static类型的变量。

目前不足的地方主要是没有区分 姓名的性别暗示,有可能组出不合乎审美的名字

 

using System;
using System.Text;

namespace ConsoleApplication1
{
    public class UserNameGenerate
    {
        private static string[] FIRST_NAME_ARRAY ={
    "","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","羿","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","宿","","怀","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","寿","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","广","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","","","","","","","","","","","","","","","","","","","","","","",
   "","","","","万俟","司马","上官","欧阳","夏侯","诸葛","闻人","东方","赫连","皇甫","羊舌","尉迟","公羊","澹台","公冶","宗正",
   "濮阳","淳于","单于","太叔","申屠","公孙","仲孙","轩辕","令狐","钟离","宇文","长孙","慕容","鲜于","闾丘","司徒","司空","兀官","司寇",
   "南门","呼延","子车","颛孙","端木","巫马","公西","漆凋","车正","壤驷","公良","拓跋","夹谷","宰父","谷梁","段干","百里","东郭","微生",
   "梁丘","左丘","东门","西门","南宫","第五","公仪","公乘","太史","仲长","叔孙","屈突","尔朱","东乡","相裡","胡母","司城","张廖","雍门",
   "毋丘","贺兰","綦毋","屋庐","独孤","南郭","北宫","王孙","", "","", "", "叱吒","","","",""};

        private static string[] LAST_NAME_ARRAY = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "广", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "鸿", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "绿", "", "", "", "", "", "", "", "", "", "", "", "", "" };

        private const int FIRST_NAME_LENGTH = 607;
        private const int LAST_NAME_LENGTH = 357;

        private static int seed = int.MinValue;

        private static int getSeed()
        {
            if (seed >= int.MaxValue)
                seed *= -1;

            seed++;

            return seed;
        }

        public static string GetName()
        {
            Random random = new Random(getSeed()); 
            StringBuilder name = new StringBuilder();
            name.AppendFormat("{0}{1}", FIRST_NAME_ARRAY[random.Next(0, FIRST_NAME_LENGTH)], LAST_NAME_ARRAY[random.Next(0, LAST_NAME_LENGTH)]);

            // 单字双字 都有50%
            bool isDoubleName = random.Next(0, 2) == 1;

            if (isDoubleName)
            {
                name.Append(LAST_NAME_ARRAY[random.Next(0, LAST_NAME_LENGTH)]);
            }
            return name.ToString();
        }
    }
}

 

举例使用方法

 for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(UserNameGenerate.GetName());
            }

 

结果:

posted @ 2014-03-19 13:50  谪仙  阅读(2766)  评论(0编辑  收藏  举报