随机生成手机号 昵称 工具类
时间: 2023-09-21 15:10:59 浏览: 106
生成随机手机号、昵称和工具类可以使用以下代码:
```python
import random
# 生成随机手机号
def generate_random_phone():
prefix_list = ['130', '131', '132', '133', '134', '135', '136', '137', '138', '139',
'150', '151', '152', '153', '155', '156', '157', '158', '159',
'180', '181', '182', '183', '184', '185', '186', '187', '188', '189']
prefix = random.choice(prefix_list)
suffix = ''.join(random.sample('0123456789', 8))
return prefix + suffix
# 生成随机昵称
def generate_random_nickname(length=8):
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
nickname = ''.join(random.choice(letters) for i in range(length))
return nickname
# 工具类
class RandomUtils:
@staticmethod
def generate_random_phone():
prefix_list = ['130', '131', '132', '133', '134', '135', '136', '137', '138', '139',
'150', '151', '152', '153', '155', '156', '157', '158', '159',
'180', '181', '182', '183', '184', '185', '186', '187', '188', '189']
prefix = random.choice(prefix_list)
suffix = ''.join(random.sample('0123456789', 8))
return prefix + suffix
@staticmethod
def generate_random_nickname(length=8):
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
nickname = ''.join(random.choice(letters) for i in range(length))
return nickname
```
你可以调用 `generate_random_phone()` 函数来生成一个随机手机号,调用 `generate_random_nickname()` 函数来生成一个随机昵称。另外,你也可以使用 `RandomUtils` 类的静态方法来达到同样的效果。
注意:这里生成的手机号和昵称都是随机生成的,可能会与现实中的实际号码和昵称重复,请谨慎使用。
阅读全文