欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Python3 生成随机手机号码

最编程 2024-04-08 14:03:44
...

log:
使用场景:接口性能测试手机号动态传参,生成低概率不重复手机号

代码如下

def create_phone():
    # 第二位数字
    second = [3, 4, 5, 7, 8][random.randint(1, 4)]
    #第三位数字
    third = {3: random.randint(0, 9),
             4: [5, 7, 9][random.randint(0, 2)],
             5: [i for i in range(10) if i != 4][random.randint(0, 8)],
             7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)],
             8: random.randint(0, 9), }[second]

    # second = [6,9][random.randint(0, 1)]
    # third = {6: random.randint(0, 9),
    #          9: random.randint(0, 9), }[second]
    # 最后八位数字
    suffix = random.randint(9999999, 100000000)
    # 拼接手机号
    return "1{}{}{}".format(second, third, suffix)