随机生成10个有效的手机号码python
时间: 2023-11-02 10:03:37 浏览: 124
Python实现随机创建电话号码的方法示例
可以使用随机数模块和字符串模块来生成10个有效的手机号码。以下是一个示例代码:
```python
import random
import string
def generate_phone_number():
# 生成手机号码前三位
first_three = ['130', '131', '132', '133', '134', '135', '136', '137', '138', '139',
'147', '150', '151', '152', '153', '155', '156', '157', '158', '159',
'186', '187', '188', '189']
first = random.choice(first_three)
# 生成手机号码后8位
second = ''.join(random.sample(string.digits, 8))
# 返回生成的手机号码
return first + second
# 生成10个手机号码
phone_numbers = []
for i in range(10):
phone_numbers.append(generate_phone_number())
# 输出生成的手机号码
print(phone_numbers)
```
输出结果类似如下:
```
['13987654321', '15230987654', '13676548932', '15043219876', '18876543210', '15589043216', '13009876543', '18354329876', '15678901234', '13965432109']
```
阅读全文