gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
用Python随机生成中国身份证号码
- User
用Python随机生成中国身份证号码
- Assistant
以下是Python代码:
pythonimport random def generate_id(): # 随机生成一个省份代码 province_code = str(random.randint(11, 91)) # 随机生成一个出生日期 year = str(random.randint(1950, 2021)).zfill(4) month = str(random.randint(1, 12)).zfill(2) day = str(random.randint(1, 28)).zfill(2) birth_date = year + month + day # 随机生成一个顺序码 seq_code = str(random.randint(1, 999)).zfill(3) # 计算校验码 id_no17 = province_code + birth_date + seq_code weight_factors = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] chk_codes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] chk_sum = sum(map(lambda x, y: int(x) * y, id_no17, weight_factors)) chk_code = chk_codes[chk_sum % 11] # 返回完整的身份证号码 return province_code + birth_date + seq_code + chk_code # 测试 for i in range(10): print(generate_id())