惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
J
Java Code Geeks
D
DataBreaches.Net
B
Blog RSS Feed
D
Docker
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Y
Y Combinator Blog
A
About on SuperTechFans
V
V2EX
罗磊的独立博客
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
月光博客
月光博客
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
阮一峰的网络日志
阮一峰的网络日志

博客园 - 在天空飞翔

asp net core 跨平台初体验 获取图片的主色调 U盘启动安装 window server 2003 扩展 DataGridView 的功能(五) WebBrowser - 想说爱你不容易 表白 天涯宝盒-天涯看贴脚本-只看楼主-自动翻页 使用 asp.net 编写的一些大中型的网站 PrecompiledApp.config 的惨剧 [音乐] the dream catcher csv 文件的读取 扩展DataGridView 的功能(四) 三八节快乐 将MP3文件嵌入到exe中并播放 [音乐] 下个路口见 雷人的面试 发现不明飞行物 扩展 DataGridView 的功能(三) 扩展 DataGridView 的功能(二)
简单的中文姓名生成器
在天空飞翔 · 2013-02-18 · via 博客园 - 在天空飞翔

项目在测试时,有时候需要生成大量的测试数据,如果是数字之类的还比较好办,直接用 Random 类随机生成就行了,如果是人名之类的就要麻烦一些,总不能把人的名字也随机生成为 abc 之类的吧,于是就有了下面这个“中文姓名生成器".

”姓“的数据从百家姓中找,”名“的资料也是网上搜搜就行了,比如男孩一般叫”强“、”国“、”浩“。。。女孩一般叫”丽“、”霞“、”燕“等等。然后把这两部分随机组合就行了。

public class NameGenerator
    {
        public string[] FirstNames { get; set; }
        public string[] BoyWords { get; set; }
        public string[] GirlWords { get; set; }

        Random _rand = new Random((int)DateTime.Now.Ticks);

        public NameGenerator()
        {
            FirstNames = new[] { "","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","", "欧阳","夏侯","皇甫","上官"};
            BoyWords = new[] { "","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
            GirlWords = new[] { "","","","","","","","","","","","","","","","","","","","","","","","",""};
        }

        public string generate(string first_name, int sex, int length)
        {
            var index = 0;
            if (string.IsNullOrEmpty(first_name))
            {
                index = _rand.Next(FirstNames.Length);
                first_name = FirstNames[index];
            }
            var name = first_name;
            if (sex < 0 || sex > 1)
            {
                sex = _rand.Next(2);
            }
            if (length < 1)
            {
                length = _rand.Next(2) + 1;
            }
            
            for (int i = 0; i < length; i++)
            {
                if (sex == 0)
                {
                    index = _rand.Next(BoyWords.Length);
                    name += BoyWords[index];
                }
                else
                {
                    index = _rand.Next(GirlWords.Length);
                    name += GirlWords[index];
                }
            }
            return name;
        }