随机产生帐号信息

最近因为一个特定需要,需要产生大量帐号信息,要求是中文姓名需要随机产生,性别需要和姓名匹配,即男性姓名的性别需要是男性,女性姓名的性别需要是女性。参考wj2929兄弟的实现,我也做了一个。

首先,定义帐号实体:

   /// <summary>
    /// 账号基本信息
    /// </summary>
    public class AccountInfo
    {
        /// <summary>
        /// 账号中文姓名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 账号对应性别
        /// </summary>
        public Sex Sex { get; set; }
        /// <summary>
        /// 账号对应的登陆名称
        /// </summary>
        public string Account { get; set; }
        /// <summary>
        /// 账号人员出生日期
        /// </summary>
        public DateTime Birthday { get; set; }
        /// <summary>
        /// 账户人员居住地
        /// </summary>
        public string City { get; set; }
        /// <summary>
        /// 账户密码
        /// </summary>
        public string Password { get; set; }
    }
    public enum Sex
    {
        男, 女
    }

再分别定义姓氏、男性姓名、女性姓名,在随机产生一个性别,并在之后随机产生一个按照性别归类的姓名即可:

    /// <summary>
    /// 随机产生帐号信息
    /// </summary>
    public class RandomAccount
    {
        static string[] strSurname = {"", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "" };
        static string[] strBoyName ={"", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "广", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "" };
        static string[] strGirlName ={"", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "", "", "", "", "", "", "", "", 
                               "", "", "" };
        /// <summary>
        /// 随机产生一个账号信息
        /// </summary>
        /// <returns>完整AccountInfo对象</returns>
        public static AccountInfo GetNew()
        {
            var accountInfo = GenerateName();
            accountInfo.Birthday = GenerateBirthday();
            accountInfo.City = GenerateCity();
            accountInfo.Password = GenerationPassword();
            return accountInfo;
        }
        /// <summary>
        /// 随机产生一个合法日期
        /// </summary>
        /// <returns></returns>
        private static DateTime GenerateBirthday()
        {
            var r = new Random();
            return new DateTime(r.Next(1960, 2008), r.Next(1, 12), r.Next(1, 32));
        }
        /// <summary>
        /// 随机产生居住地
        /// </summary>
        /// <returns>城市名称</returns>
        private static string GenerateCity()
        {
            return String.Empty;
        }
        /// <summary>
        /// 随机产生密码
        /// </summary>
        /// <returns></returns>
        private static string GenerationPassword()
        {
            return Guid.NewGuid().ToString().Substring(0, new Random().Next(6, 20));
        }
        /// <summary>
        /// 随机产生一个人名称
        /// </summary>
        /// <returns>AccountInfo对象</returns>
        private static AccountInfo GenerateName()
        {
            int nPername;
            string strFullName;
            var nRan = new Random();
            var nSurname = nRan.Next(0, strSurname.Length);
            var nNum = nRan.Next(2, 4);
            var nSex = nRan.Next(0, 2);
            var accountInfo = new AccountInfo();
            if (nSex == 0)
            {
                accountInfo.Sex = Sex.男;
                //rbMale.Checked = true;
                nPername = nRan.Next(0, strBoyName.Length);
                strFullName = strSurname[nSurname] + strBoyName[nPername];
                if (nNum == 3)
                {
                    nPername = nRan.Next(0, strBoyName.Length);
                    strFullName = strFullName + strBoyName[nPername];
                }
                accountInfo.Name = strFullName;
                //accountInfo.Account = Helper.ChineseSpell.MakeSpellCode(strFullName,0);
                //tbFullName.Text = strFullName;
            }
            else
            {
                accountInfo.Sex = Sex.女;
                //rbFemale.Checked = true;
                nPername = nRan.Next(0, strGirlName.Length);
                strFullName = strSurname[nSurname] + strGirlName[nPername];
                if (nNum == 3)
                {
                    nPername = nRan.Next(0, strGirlName.Length);
                    strFullName = strFullName + strGirlName[nPername];
                }
                accountInfo.Name = strFullName;
                //accountInfo.Account = Helper.ChineseSpell.MakeSpellCode(strFullName, 0);
                //tbFullName.Text = strFullName;
            }
            return accountInfo;
        }
    }

其中为了随机产生和姓名一致的帐号登陆名称,我用到了将汉字转化为拼音的一个类,这个类可以在cnblogs上面找到。

这样一个基本完整的帐号信息就产生了,在实际应用中,我还根据帐号名称检查了帐号是否存在,如果已经存在则在Account属性后面不上随机数字来保证帐号不重复。