python随机设置身份证号
时间: 2023-07-01 11:24:19 浏览: 55
可以使用 Python 的 random 模块生成随机数,然后按照身份证号的规则进行拼接。以下是一个示例代码:
```python
import random
# 随机生成一个地区码,前六位数字
region_code = str(random.randint(110000, 659004))
# 随机生成一个出生日期,中间八位数字
birth_year = str(random.randint(1950, 2021))
birth_month = str(random.randint(1, 12)).zfill(2)
if birth_month == '02':
birth_day = str(random.randint(1, 28)).zfill(2)
else:
birth_day = str(random.randint(1, 30)).zfill(2)
birth_date = birth_year + birth_month + birth_day
# 随机生成一个顺序码,最后四位数字
sequence_code = str(random.randint(0, 9999)).zfill(4)
# 计算校验码
id_number = region_code + birth_date + sequence_code
weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
checksums = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
checksum = 0
for i in range(len(id_number)):
checksum += int(id_number[i]) * weights[i]
checksum %= 11
checksum = checksums[checksum]
# 拼接出最终的身份证号
id_number += checksum
print(id_number)
```
请注意,这个身份证号仅供学习和参考使用,不得用于非法用途。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)