Skip to content

Instantly share code, notes, and snippets.

@ichengzi
Created July 20, 2022 09:36
Show Gist options
  • Save ichengzi/f90abb89c12d156a8582a19389b058a1 to your computer and use it in GitHub Desktop.
Save ichengzi/f90abb89c12d156a8582a19389b058a1 to your computer and use it in GitHub Desktop.
c# 随机身份证生成器
public class RandomIdCardUtil
{
[Test]
public void generate()
{
for (int i = 0; i < 100; i++)
{
var cusId = RandomIDCardGenerator.GetRandomCard(14, 14);
Console.WriteLine($"{cusId.Substring(0, 6)}" +
$"-{cusId.Substring(6, 4)}" +
$"-{cusId.Substring(10, 2)}" +
$"-{cusId.Substring(12, 2)}" +
$"-{cusId.Substring(14)}");
}
}
}
public static class RandomIDCardGenerator
{
/*
* 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",
* 21:"辽宁",22:"吉林",23:"黑龙江",
* 31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",
* 41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",
* 50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",
* 61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",
* 71:"台湾",81:"香港",82:"澳门",91:"国外"
*/
private static String[] PROVINCES =
{
"11", "12", "13", "14", "15",
"21", "22", "23",
"31", "32", "33", "34", "35", "36", "37",
"41", "42", "43", "44", "45", "46",
"50", "51", "52", "53", "54",
"61", "62", "63", "64", "65",
"71", "81", "82"
};
private static String[] CITYS =
{
"01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
"21", "22", "23", "24", "25", "26", "27", "28"
};
private static String[] COUNTYS =
{
"01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
"31", "32", "33", "34", "35", "36", "37", "38"
};
private static char[] CHECKS_CODE = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
private static int[] POWER_LIST = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
// 18位身份证号码各位的含义:
// 1-2位省、自治区、直辖市代码;
// 3-4位地级市、盟、自治州代码;
// 5-6位县、县级市、区代码;
// 7-14位出生年月日,比如19670401代表1967年4月1日;
// 15-17位为顺序号,其中17位(倒数第二位)男为单数,女为双数;
// 18位为校验码,0-9和X。
// 作为尾号的校验码,是由把前十七位数字带入统一的公式计算出来的,
// 计算的结果是0-10,如果某人的尾号是0-9,都不会出现X,但如果尾号是10,那么就得用X来代替,
// 因为如果用10做尾号,那么此人的身份证就变成了19位。X是罗马数字的10,用X来代替10
public static String GetRandomCard(int minAge, int maxAge)
{
Random random = new Random();
// 随机生成省、自治区、直辖市代码 1-2
String province = PROVINCES[random.Next(PROVINCES.Length - 1)];
// 随机生成地级市、盟、自治州代码 3-4
String city = CITYS[random.Next(CITYS.Length - 1)];
// 随机生成县、县级市、区代码 5-6
String county = COUNTYS[random.Next(COUNTYS.Length - 1)];
// 随机生成出生年月 7-14
String birth = RandomBirthDay(minAge, maxAge);
// 随机生成顺序号 15-17
String no = random.Next(999).ToString().PadLeft(3, '0');
// 随机生成校验码 18
String tmp = province + city + county + birth + no;
char[] tmpArray = tmp.ToCharArray();
int power = 0;
for (int i = 0; i < tmpArray.Length; i++)
{
power += (tmpArray[i] - '0') * POWER_LIST[i];
}
char lastCheckChar = CHECKS_CODE[power % 11];
return tmp + lastCheckChar;
}
private static String RandomBirthDay(int minAge, int maxAge)
{
int span = maxAge - minAge;
span = Math.Abs(span);
if (span == 0)
span = 1;
int random = new Random().Next(span * 365);
var birthDay = DateTime.Today.AddYears(-minAge).AddDays(-random);
return birthDay.ToString("yyyyMMdd");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment