美文网首页
2018-03-29 C# 生成随机身份证号码

2018-03-29 C# 生成随机身份证号码

作者: 黑夜与繁星 | 来源:发表于2018-03-29 16:23 被阅读73次

参考了这篇作者的思路:https://blog.csdn.net/xiaokui_wingfly/article/details/45893791

/*
*为了方便,我将地区设定在了110000 北京。
地区也是可以随机,但是它的数据集太多,我偷懒没有写,如有兴趣可以自行补上。
*/
  class Program
    {
//生成随机年份
        public static int generateYear()
        {
            StringBuilder bu = new StringBuilder();
            Random rd = new Random();
            int year = rd.Next(1900, DateTime.Now.Year);
            bu.Append(year);
            return int.Parse(bu.ToString());
        }
//生成
        public static string generate()
        {
            StringBuilder bu = new StringBuilder();
            Random rd = new Random();
            bu.Append(110000);
            int year = generateYear();
            bu.Append(year);
            int month = rd.Next(1, 12);
            if (month < 10)
            {
                bu.Append(0);
            }
            bu.Append(month);
            int[] days;
            if (isleapyear(year))
            {
                days =new int[12]{31,29,31,30,31,30,31,31,30,31,30,31};
            }
            else
            {
                days = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            }

            int day = rd.Next(1, days[month]);
            if (day < 10)
            {
                bu.Append(0);
            }
            bu.Append(day);
            bu.Append(randomcode());
            int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
            string convert = bu.ToString();

            int it = 0;
            int res =0;
          //  Console.WriteLine(res);

            while (it<17)
            {
                res = res + (convert[it] - '0') * c[it];
             //   Console.WriteLine("第"+it+"次,"+convert[it]+"乘以"+c[it]);
            //    Console.WriteLine(res);
                it++;
            }


            int i = res%11;
            bu.Append(r[i]);
            return bu.ToString();
        }
//生成3位随机
        public static string randomcode()
        {
            StringBuilder bu = new StringBuilder();
            Random rd = new Random();
            int code = rd.Next(1, 999);
            if (code < 10)
            {
                bu.Append(00);
            }else if (code < 100)
            {
                bu.Append(0);
            }
            bu.Append(code);
            return bu.ToString();
        }


        public static bool isleapyear(int year)
        {
            if (year % 4 == 0)
            {
                if(year % 100 == 0 && year % 400 != 0)
                {
                    return false;
                }
                return true;
            }
            return false;
        }


        static void Main(string[] args)
        {          
            Console.Write(generate());
            Console.ReadKey();
        }
    }
image.png

验证
将程序输出的结果拿到验证网站上面检测是合法号码


相关文章

网友评论

      本文标题:2018-03-29 C# 生成随机身份证号码

      本文链接:https://www.haomeiwen.com/subject/etzmcftx.html